我是新來的春天,我目前正在努力獲取多部分表單提交/驗證方案,並在視圖中顯示錯誤信息。春季多部分文件上傳表單驗證
這裏有文件,我目前有:
resourceupload.jsp:顯示一個表單,上傳文件的視圖。
<form:form method="post" action="resource/upload" enctype="mutlipart/form-data">
<input name="name" type="text"/>
<input name="file" type="file" />
<input type="submit"/>
<form:errors path="file" cssClass="errors"/>
</form>
resourceuploadcontroller.java:處理表單提交,和(unsuccessfuly)嘗試發佈文件驗證錯誤回視圖控制器:
@RequestMapping(method = RequestMethod.POST)
public String handleFormUpload(@RequestParam("file") MultipartFile file , @RequestParam("name") String name,Object command, Errors validationErrors){
..perform some stuff with the file content, checking things in the database, etc...
.. calling validationErrors.reject("file","the error") everytime something goes wrong...
return "redirect:upload"; // redirect to the form, that should display the error messages
現在,很明顯這裏有些不對勁這種方法:
1 /我不得不在validationErrors參數前添加一個虛擬的「command」對象,否則spring會給我一個錯誤。這看起來並不正確。
2 /添加該參數後,重定向不會將錯誤傳遞給視圖。我試圖在控制器的開始使用@SessionAttribute(「文件」),沒有任何運氣。
如果任何人都可以幫助...我有一看註釋@ResponseBody,但似乎並沒有做出與視圖中使用..