DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
View Events Video Library
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Mobile Database Essentials: Assess data needs, storage requirements, and more when leveraging databases for cloud and edge applications.

Full-Stack Observability Essentials: Explore the fundamentals of system-wide observability and key components of the OpenTelemetry standard.

Monitoring and Observability for LLMs: Datadog and Google Cloud discuss how to achieve optimal AI model performance.

Automated Testing: The latest on architecture, TDD, and the benefits of AI and low-code tools.

Related

  • Spring Boot File Upload Example With MultipartFile | Spring Boot Tutorial
  • Spring Boot File Upload Example With MultipartFile [Video]
  • Upload and Retrieve Files/Images Using Spring Boot, Angular, and MySQL
  • Embracing Reactive Programming With Spring WebFlux

Trending

  • Five Free AI Tools for Programmers to 10X Their Productivity
  • Streamlined Infrastructure Deployment: Harnessing the Power of Terraform and Feature Toggles
  • Beyond the Prompt: Unmasking Prompt Injections in Large Language Models
  • [DZone Research] Join Us for Our 5th Annual Kubernetes Survey!
  1. DZone
  2. Coding
  3. Frameworks
  4. ExtJS 4 File Upload + Spring MVC 3 Example

ExtJS 4 File Upload + Spring MVC 3 Example

Loiane Groner user avatar by
Loiane Groner
·
Jul. 21, 11 · Interview
Like (0)
Save
Tweet
Share
69.78K Views

Join the DZone community and get the full member experience.

Join For Free

this 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/

Spring Framework Upload Ext JS

Opinions expressed by DZone contributors are their own.

Related

  • Spring Boot File Upload Example With MultipartFile | Spring Boot Tutorial
  • Spring Boot File Upload Example With MultipartFile [Video]
  • Upload and Retrieve Files/Images Using Spring Boot, Angular, and MySQL
  • Embracing Reactive Programming With Spring WebFlux

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: