ExtJS 4 File Upload + Spring MVC 3 Example
Join the DZone community and get the full member experience.
Join For Freethis tutorial will walk you through out how to use the ext js 4 file upload field in the front end and spring mvc 3 in the back end.
this tutorial is also an update for the tutorial ajax file upload with extjs and spring framework , implemented with ext js 3 and spring mvc 2.5.
ext js file upload form
first, we will need the ext js 4 file upload form. this one is the same as showed in ext js 4 docs .
ext.onready(function(){ ext.create('ext.form.panel', { title: 'file uploader', width: 400, bodypadding: 10, frame: true, renderto: 'fi-form', items: [{ xtype: 'filefield', name: 'file', fieldlabel: 'file', labelwidth: 50, msgtarget: 'side', allowblank: false, anchor: '100%', buttontext: 'select a file...' }], buttons: [{ text: 'upload', handler: function() { var form = this.up('form').getform(); if(form.isvalid()){ form.submit({ url: 'upload.action', waitmsg: 'uploading your file...', success: function(fp, o) { ext.msg.alert('success', 'your file has been uploaded.'); } }); } } }] }); });
html page
then in the html page, we will have a div where we are going to render the ext js form. this page also contains the required javascript imports
<html> <head> <title>spring fileupload example with <a target="_blank" title="extjs" href="http://sencha.com/">extjs</a> 4 form</title> <!-- <a target="_blank" title="ext js" href="http://sencha.com/">ext js</a> files --> <link rel="stylesheet" type="text/css" href="/extjs4-file-upload-spring/<a target="_blank" title="extjs" href="http://sencha.com/">extjs</a>/resources/css/ext-all.css" /> <script type="text/javascript" src="/extjs4-file-upload-spring/<a target="_blank" title="extjs" href="http://sencha.com/">extjs</a>/bootstrap.js"></script> <!-- file upload form --> <script src="/extjs4-file-upload-spring/js/file-upload.js"></script> </head> <body> <p>click on "browse" button (image) to select a file and click on upload button</p> <div id="fi-form" style="padding:25px;"></div> </body> </html>
fileupload bean
we will also need a fileupload bean to represent the file as a multipart file:
package com.loiane.model; import org.springframework.web.multipart.commons.commonsmultipartfile; /** * represents file uploaded from <a target="_blank" title="extjs" href="http://sencha.com/">extjs</a> form * * @author loiane groner * http://loiane.com * http://loianegroner.com */ public class fileuploadbean { private commonsmultipartfile file; public commonsmultipartfile getfile() { return file; } public void setfile(commonsmultipartfile file) { this.file = file; } }
file upload controller
then we will need a controller. this one is implemented with spring mvc 3.
package com.loiane.controller; import org.springframework.stereotype.controller; import org.springframework.validation.bindingresult; import org.springframework.validation.objecterror; import org.springframework.web.bind.annotation.requestmapping; import org.springframework.web.bind.annotation.requestmethod; import org.springframework.web.bind.annotation.responsebody; import com.loiane.model.extjsformresult; import com.loiane.model.fileuploadbean; /** * controller - spring * * @author loiane groner * http://loiane.com * http://loianegroner.com */ @controller @requestmapping(value = "/upload.action") public class fileuploadcontroller { @requestmapping(method = requestmethod.post) public @responsebody string create(fileuploadbean uploaditem, bindingresult result){ extjsformresult extjsformresult = new extjsformresult(); if (result.haserrors()){ for(objecterror error : result.getallerrors()){ system.err.println("error: " + error.getcode() + " - " + error.getdefaultmessage()); } //set <a target="_blank" title="extjs" href="http://sencha.com/">extjs</a> return - error extjsformresult.setsuccess(false); return extjsformresult.tostring(); } // some type of file processing... system.err.println("-------------------------------------------"); system.err.println("test upload: " + uploaditem.getfile().getoriginalfilename()); system.err.println("-------------------------------------------"); //set <a target="_blank" title="extjs" href="http://sencha.com/">extjs</a> return - sucsess extjsformresult.setsuccess(true); return extjsformresult.tostring(); }
ext js form return
some people asked me how to return something to the form to display a message to the user. we can implement a pojo with a success property. the success property is the only thing ext js needs as a return:
package com.loiane.model; /** * a simple return message for <a target="_blank" title="ext js" href="http://sencha.com/">ext js</a> * * @author loiane groner * http://loiane.com * http://loianegroner.com */ public class extjsformresult { private boolean success; public boolean issuccess() { return success; } public void setsuccess(boolean success) { this.success = success; } public string tostring(){ return "{success:"+this.success+"}"; } }
spring config
don’t forget to add the multipart file config in the spring config file:
<!-- configure the multipart resolver --> <bean id="multipartresolver" class="org.springframework.web.multipart.commons.commonsmultipartresolver"> <!-- one of the properties available; the maximum file size in bytes --> <property name="maxuploadsize" value="100000"/> </bean>
nullpointerexception
i also got some questions about nullpointerexception. make sure the fileupload field name has the same name as the commonsmultipartfile property in the fileuploadbean class:
extjs :
{ xtype: 'filefield', name: 'file', fieldlabel: 'file', labelwidth: 50, msgtarget: 'side', allowblank: false, anchor: '100%', buttontext: 'select a file...' }
java:
public class fileuploadbean { private commonsmultipartfile file; }
these properties always have to match!
you can still use the spring mvc 2.5 code with the ext js 4 code presented in this tutorial.
download
you can download the source code from my github repository (you can clone the project or you can click on the download button on the upper right corner of the project page): https://github.com/loiane/extjs4-file-upload-spring
you can also download the source code form the google code repository: http://code.google.com/p/extjs4-file-upload-spring/
both repositories have the same source. google code is just an alternative.
happy coding!
from http://loianegroner.com/2011/07/extjs-4-file-upload-spring-mvc-3-example/
Opinions expressed by DZone contributors are their own.
Comments