我是 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 网站。但尽管如此,我什么也找不到。
感谢您在这方面的帮助。