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
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
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

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workkloads.

Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Spring Boot File Upload Example With MultipartFile [Video]
  • Upload and Retrieve Files/Images Using Spring Boot, Angular, and MySQL
  • How Spring and Hibernate Simplify Web and Database Management
  • Functional Endpoints: Alternative to Controllers in WebFlux

Trending

  • Rust and WebAssembly: Unlocking High-Performance Web Apps
  • Debugging Core Dump Files on Linux - A Detailed Guide
  • How to Convert Between PDF and TIFF in Java
  • SQL Server Index Optimization Strategies: Best Practices with Ola Hallengren’s Scripts
  1. DZone
  2. Coding
  3. Frameworks
  4. ExtJS 4 File Upload + Spring MVC 3 Example

ExtJS 4 File Upload + Spring MVC 3 Example

By 
Loiane Groner user avatar
Loiane Groner
·
Jul. 21, 11 · Interview
Likes (0)
Comment
Save
Tweet
Share
71.0K 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 [Video]
  • Upload and Retrieve Files/Images Using Spring Boot, Angular, and MySQL
  • How Spring and Hibernate Simplify Web and Database Management
  • Functional Endpoints: Alternative to Controllers in WebFlux

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • 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:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!