0

我是 Spring 的初学者,我想知道为什么我的 ModelAttribute 没有填充(所有值都是空的)

我想创建一个允许我上传 csv 文件以及 CSV 文件类型的多部分。

我的代码是这样的:

在 CSVUpload-portlet.xml 中:

<bean class="org.springframework.web.portlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>
<bean class="org.springframework.web.portlet.mvc.annotation.AnnotationMethodHandlerAdapter">

<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    <property name="maxUploadSize" value="100000000"></property>
</bean>

<bean id="viewResolver"
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="viewClass"
        value="org.springframework.web.servlet.view.JstlView" />
    <property name="prefix" value="/WEB-INF/jsp/" />
    <property name="suffix" value=".jsp" />
</bean>

CSVUploadPortlet.java(控制器)

*All imports 

@Controller("uploadCsvToDatabase")
@RequestMapping(value = "VIEW")
public class CSVUploadPortlet {
  private static final Logger log = Logger.getLogger(CSVUploadPortlet.class);
  private static IWBCSVUploadRemote csvupload;

  @RenderMapping
  public String viewCSVUploadBase(Model model, RenderRequest request) {
    try {

    } catch (Exception e) {
      e.printStackTrace();

      log.info("problem in retrieving the CSV Upload service" + e);
    }
    return "csvupload/csvupload_view";
  }

  @ModelAttribute("csvFileUploadVO")
  public CSVFileUploadVO getCommandObject() 
  {
    System.out.println("SpringFileController -> getCommandObject -> Building VO");
    return new CSVFileUploadVO();
  }

  @ActionMapping(params="action=uploadCsvToDatabase")
  public void uploadCsvToDatabase(
      @ModelAttribute("csvFileUploadVO") CSVFileUploadVO csvFileUploadVO, BindingResult result, ActionRequest request, ActionResponse response, SessionStatus sessionStatus){
    try{
      System.out.println("FileType:"+csvFileUploadVO.getFileType()); //This returns null
      System.out.println("CSVFile Size:"+csvFileUploadVO.getCsvFile().getSize());  //This returns a null-pointer exception
    } catch (Exception e) {
      log.info("Problem in retrieving the CSVUpload configuration list " + e);
      e.printStackTrace();
    }
  }
}

CSVFileUploadVO.java

import org.springframework.web.multipart.commons.CommonsMultipartFile;

public class CSVFileUploadVO {
  private String fileType;
  private CommonsMultipartFile csvFile;
  private String message;

  public CSVFileUploadVO() {
  }

  public String getFileType() {
    return fileType;
  }

  public void setFileType(String fileType) {
    this.fileType = fileType;
  }

  public String getMessage() {
          return message;
  }

  public void setMessage(String message) {
          this.message = message;
  }

  public CommonsMultipartFile getCsvFile() {
          return csvFile;
  }

  public void setCsvFile(CommonsMultipartFile csvFile) {
          this.csvFile = csvFile;
  }
}

表单 JSP

All taglibs imported...
<portlet:actionURL var="fileUploadURL">
    <portlet:param name="action" value="uploadCsvToDatabase" />
</portlet:actionURL>

<form:form method="post" action="${fileUploadURL}"
    commandName="csvFileUploadVO" enctype="multipart/form-data">
    <table>
        <tbody>
            <tr>
                <td><label>Department:</label></td>
                <td><form:select path="fileType">
                        <form:option value="BRMAdd" label="BRM Add" />
                        <form:option value="FOSAdmin" label="FOS Admin" />
                        <form:option value="FOSRM" label="FOS RM" />
                        <form:option value="FOSTeam" label="FOS Team" />
                        <form:option value="ITRelationships" label="IT Relationships" />
                        <form:option value="HRAttendance" label="HR Attendance" />
                        <form:option value="iCareCallReport" label="iCare Call Report" />
                    </form:select></td>
            </tr>
            <tr>
                <td><label>Specify your File:</label></td>
                <td><form:input path="csvFile" type="file" /></td>
            </tr>
            <tr>
            <tr>
                <td colspan="100%"><input type="submit" value="Submit" /></td>
            </tr>
            <tr>
                <td colspan="100%">${csvFileUploadVO.message}</td>
            </tr>
        </tbody>
    </table>
</form:form>

我知道这似乎是在要求你帮我解决一个问题,但我已经坚持了 8 个小时,阅读了我可以用谷歌搜索的所有资源和 stackoverflow 网站。但尽管如此,我什么也找不到。

感谢您在这方面的帮助。

4

4 回答 4

0

您必须在 JSP 表单中定义 ModelAttribute,如下所示:

<form:form method="post" action="${fileUploadURL}"
modelAttribute="csvFileUploadVO" enctype="multipart/form-data">
于 2013-02-13T16:15:41.657 回答
0

Liferay 6.0.5 和 Spring 3.2.2.RELEASE:

我尝试编辑弹簧类,但这个问题有更简单的解决方案。只需检查您的 CommonsPortletMultipartResolver 是否真的已被初始化。它在 spring.xml 中声明的事实并不意味着它已被加载。

如果未初始化,则请求的文件部分将永远不会转换为 CommonsMultipartFile,因为您的请求不会是 commons Multipart 请求。

CommonsPortletMultipartResolver 将被 org.springframework.web.portlet.DispatcherPortlet 生命周期调用,如果它被加载并且 spring 使用这个类。

于 2013-05-30T11:04:05.907 回答
0

您将需要查看http://static.springsource.org/spring/docs/3.0.x/reference/mvc.html#mvc-multipart-resolver特别是RequestMapping参数所在的位置MultipartFile

@RequestMapping(value = "/form", method = RequestMethod.POST)
  public String handleFormUpload(@RequestParam("name") String name,
    @RequestParam("file") MultipartFile file) {
于 2012-09-20T16:56:54.410 回答
0

您的多部分解析器必须具有 idportletMultipartResolver并且是类型org.springframework.web.portlet.multipart.CommonsPortletMultipartResolver

<bean id="portletMultipartResolver" class="org.springframework.web.portlet.multipart.CommonsPortletMultipartResolver">
</bean>
于 2017-12-19T12:06:59.813 回答