I am new to spring and am currently struggling with many of the parts needed to get a submit / check multiple form script with the error displayed in the view.
Here are the files that I have:
resourceupload.jsp: view showing the form for uploading a file.
<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: a controller that processes the submit form, and (unsuccessfully) tries to send file validation errors back to the view:
@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";
Now, obviously, something is wrong with this approach:
1 / I had to add a dummy "command" object before the validationErrors parameter, otherwise spring will throw an error. This does not seem really correct.
2 / After I added this parameter, the redirection does not pass errors to the view. I tried using @SessionAttribute ("file") at the beginning of the controller, with no luck.
If anyone can help ... I looked at the @ResponseBody annotation, but this does not seem to be used for use with views.