Hi, a newbie question: Any example of MultipartResolver? Or it could be: what did I do wrong?
In summary, I followed Ch 12.8 of the reference, and the following things went wrong:
- in my SimpleFormController, onSubmit was never called. None of the 3 onSubmit functions were called. Only processFormSubmission was called
- in the function processFormSubmission, I can get the "simple" properties like String, but cannot see the file. It is NULL
Here's what I did:
(1) edit the servlet.xml:
(2) created a simple Bean to test:Code:<bean id="fileUploadController" class="web.FileUploadController"> <property name="commandClass"><value>web.FileUploadBean</value></property> <property name="formView"><value>uploadReport</value></property> <property name="successView"><value>uploadConfirmation</value></property> </bean> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="maxUploadSize"><value>1000000000</value></property> </bean>
(3) created FileUploadeControllerCode:public class FileUploadBean { private byte[] file; private String username; public void setFile(byte[] file) { this.file = file; } public byte[] getFile() { return file; } /** * @return Returns the username. */ public String getUsername() { return username; } /** * @param username The username to set. */ public void setUsername(String username) { this.username = username; } }
(4) create the uploadReport.jspCode:public class FileUploadController extends SimpleFormController { protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws ServletException { System.out.println("pre initBinder"); binder.registerCustomEditor(byte[].class, "file", new ByteArrayMultipartFileEditor()); System.out.println("post initBinder"); } protected boolean isFormSubmission(HttpServletRequest arg0) { System.out.println("isFormSubmission: "+arg0); // TODO Auto-generated method stub return super.isFormSubmission(arg0); } protected ModelAndView processFormSubmission(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, BindException arg3) throws Exception { System.out.println("processFormSubmission"); System.out.println("request = "+arg0); System.out.println("response = "+arg1); System.out.println("object = "+arg2); System.out.println("exception = "+arg3); FileUploadBean fub = (FileUploadBean)arg2; System.out.println("fub = "+fub); System.out.println("fub username "+fub.getUsername()); System.out.println("fub file "+fub.getFile()); // TODO Auto-generated method stub return super.processFormSubmission(arg0, arg1, arg2, arg3); } protected ModelAndView showForm(HttpServletRequest arg0, HttpServletResponse arg1, BindException arg2) throws Exception { System.out.println("showForm"); // TODO Auto-generated method stub return super.showForm(arg0, arg1, arg2); } protected void doSubmitAction(Object arg0) throws Exception { System.out.println("doSubmitAction"); // TODO Auto-generated method stub super.doSubmitAction(arg0); } protected ModelAndView onSubmit(Object arg0) throws Exception { System.out.println("onSubmit 1 arg"); // TODO Auto-generated method stub return super.onSubmit(arg0); } protected ModelAndView onSubmit(Object arg0, BindException arg1) throws Exception { System.out.println("onSubmit 2 args"); // TODO Auto-generated method stub return super.onSubmit(arg0, arg1); } protected ModelAndView onSubmit(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, BindException arg3) throws Exception { System.out.println("onSubmit 4 args"); // TODO Auto-generated method stub return super.onSubmit(arg0, arg1, arg2, arg3); } }
Code:<html> <body> <h1>Upload a file</h1> <form method="POST" action="upload.form" encrypt="multipart/form-data"> <input type="file" name="file"/> <input name="username" value="Test User"/><br> <input type="submit"/> </form> </body> </html>
Sorry if this seem a little entry-level, but I have spent hours googling and reading the javadoc and still have no idea where to go next. Any pointers will be greatly appreciated!
Paul


Reply With Quote