我是Spring的初学者,我想知道为什么我的ModelAttribute没有填充(所有值都为null)
我想创建一个多部分,允许我上传一个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网站,我可以google。但尽管如此,我还是找不到任何东西。
感谢您的帮助。
答案 0 :(得分:0)
您需要查看http://static.springsource.org/spring/docs/3.0.x/reference/mvc.html#mvc-multipart-resolver,尤其是参数为RequestMapping
的{{1}}
MultipartFile
答案 1 :(得分:0)
您必须在JSP表单中定义ModelAttribute,如下所示:
<form:form method="post" action="${fileUploadURL}"
modelAttribute="csvFileUploadVO" enctype="multipart/form-data">
答案 2 :(得分:0)
Liferay 6.0.5和Spring 3.2.2.RELEASE:
我尝试过编辑spring类,但是解决这个问题还有更简单的方法。只需检查您的CommonsPortletMultipartResolver是否真的已经启动。它在spring.xml中声明的事实并不意味着它已被加载。
如果未初始化,则请求的文件部分永远不会转换为CommonsMultipartFile,因为您的请求不会是公共Multipart请求。
CommonsPortletMultipartResolver将由org.springframework.web.portlet.DispatcherPortlet生命周期调用,如果它已加载并且spring使用此类。
答案 3 :(得分:0)
您的多部分解析程序的ID必须为portletMultipartResolver
且类型为org.springframework.web.portlet.multipart.CommonsPortletMultipartResolver
<bean id="portletMultipartResolver" class="org.springframework.web.portlet.multipart.CommonsPortletMultipartResolver">
</bean>