我对Spring框架不熟悉,目前在处理多部分表单提交/验证场景时遇到了很多问题,特别是在视图中显示错误信息方面。
这是我目前拥有的文件:
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:处理表单提交的控制器,并(不成功地)尝试将文件验证错误返回到视图:
@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("file"),但没有任何运气。
如果有人能够帮忙...我看了一下@ResponseBody注释,但那似乎不是用于视图的。