Ajax file upload in Struts2 - Using Ajax File Upload Plugin

January 19th, 2008 | by Admin |

Download Source | View Demo

One of the advanced features of Struts2 is its plugin support. By using a standard interface API, you can seamlessly extend Struts2 capability.

Plugins are packaged as jar files along with any dependency jars. To use a plugin, you need to add it to the classpath. Struts automatically configures a plugin by using struts-plugin.xml packaged as part of the plugin jar.

When you use external plugins (plugins which are not part of standard Struts2 distribution) you must be aware of the risks. The plugin may not be fully tested and there could be other hidden issues. So always evaluate an external plugin before using it in a production server.

In this post, I will look at a very useful external Struts2 plugin - Monitored Ajax File Upload. Here is are the steps required for a sample demo.

yui-and-struts2-ajax-upload-plugin.jpg

1. Download Ajax File Upload plugin - You can download this from here.

2. Extract the zip and then copy the jars to WEB-INF/lib of your Web application. The jars are json-lib-2.0, commons-io-1.3.1, commons-fileupload-1.2 and AjaxFileUpload-0.03

3. Now create a FileUploadDemoAction action class extending from com.davidjc.ajaxfileupload.action.FileUpload. This is given below.

4. Configure struts.xml with the action and along with an additional interceptor as given below.

5. Create a JSP which contains file upload control. You need to use the ajaxfileuploadform custom tag. In this example, this jsp is ajaxupload.jsp.

6. This added files are displayed below the upload control using Ajax. For this YUI component connect is used for the GET request. The request is made to FileUploadDetailsAction and the results are returned using uploadlist.jsp. This content is added to the div tag (with id “did”) in the main page. Note how the ajax call is done from show_list method. This method is given as doafter parameter which is a JavaScript function executed immediately after file upload.

The above code also illustrates usage OGNL expressions and the use of status attribute on iterator tag for handling even/odd rows.

7. Configure the server location of the uploaded temporary file by setting struts.multipart.saveDir property. This can be done in struts.properties or web.xml. In this sample, it is set to c:/temp (windows machine) in web.xml. Make sure that the directory exists on the filesystem. In this example I have also set the maximum upload file size to 1MB using struts.multipart.maxSize property.

Complete details of the uploaded file is available in the FileUploadDemoAction. The temporary file created during upload is deleted after sometime. So you can use the File object to save the content to a separate folder or to a database.

To test this sample, use the JSP URL directly : http://localhost:8080/struts2demo/ajaxupload.jsp

Important: Ensure that you have Log4J jar in your application server’s lib folder.Also I am aware that this example gives errors and is not properly showing progress bar. I am still investigating these problems.

Got any questions? Don’t hesitate to contact me. Greetings to Dave Casserly for this excellent plugin.

5 Responses to “Ajax file upload in Struts2 - Using Ajax File Upload Plugin”

  1. By Jignesh on Feb 10, 2008

    This software works fine. But I want to customize according to my requirement. I don’t want to remove old file. Moreover when it uploads file it doesn’t store orginal name but gives names like:upload_345931f1_118047ef8d8__8000_00000000.tmp.

    How to get the code for FileUpload.java class to customize.

  2. By Cesare on Mar 11, 2008

    I’ve the same problem !
    please hel us!

    thanks.

  3. By Dave Casserly on Mar 27, 2008

    Hi,

    You get a handle to the actual file in your action. This .tmp file is created by the apache fileupload library.

    Dave

  4. By Cristian Olaru on Apr 1, 2008

    I have this error first time I acces this JSP: http://localhost:8080/struts2demo/ajaxupload.jsp

    Do you have any idea why. Is something in AjaxFileUpload-0.03 plugin. Thanks!

    …….
    FreeMarker template error!

    Error on line 27, column 20 in template/simple/head.ftl
    Expecting a string, date or number here, Expression parameters.debug?default(false) is instead a freemarker.template.TemplateBooleanModel$1
    The problematic instruction:
    ———-
    ==> ${parameters.debug?default(false)} [on line 27, column 18 in template/simple/head.ftl]
    in include “/${parameters.templateDir}/simple/head.ftl” [on line 24, column 1 in template/xhtml/head.ftl]

  5. By Sharath on Apr 11, 2008

    Hi All,

    I’m trying this example but I’m getting below error, Please let me know what I’m doing wrong.

    FreeMarker template error!

    Error on line 27, column 20 in template/simple/head.ftl
    Expecting a string, date or number here, Expression parameters.debug?default(false) is instead a freemarker.template.TemplateBooleanModel$1
    The problematic instruction:
    ———-
    ==> ${parameters.debug?default(false)} [on line 27, column 18 in template/simple/head.ftl]
    ———-

    Java backtrace for programmers:
    ———-

    Thanks in advance.

    Regards,
    Sharath Karnati.

Post a Comment