Thursday, June 20, 2013

ExtJs 4 with Spring MVC: Fetching and displaying JSON data in grid panel

ExtJs 4 with Spring MVC: Fetching and displaying JSON data in grid Ext JS is a JavaScript framework for client side MVC. Its modern UI widgets help to render native desktop application like user interface. It can be integrated with any server side technology such as PHP, JSP or ASP to develop enterprise application. There are many tutorials that talk about Ext Js integration with PHP but not many bloggers have talked about integration of Ext Js with spring framework which can be used for implementing server side MVC. In this tutorial I’ll show how to create a grid panelwith json data. For this tutorial I have used eclipse IDE and Tomcat 6 as server.

  • Maven
  • Spring
  • Some background knowledge of how ExtJs works. 
As you might be expecting start with creating a new Dynamic web project in Eclipse. Convert it to Maven project and add following dependencies for including spring to your pom.xml


Now edit your web.xml file to configure your servlet. Here is the content of my web.xml file

 <web-app id="WebApp_ID" version="2.4"  
      xmlns="" xmlns:xsi=""  
      <display-name>Spring with Extjs MVC Application</display-name>  

In mvc-dispatcher-servlet.xml define your view resolver, base package for controller class and also create a mapping for all static resources {required to include JavaScript & CSS files for Ext JS}

 <context:component-scan base-package="com.abhas.controller" />  
           <property name="prefix">  
           <property name="suffix">  
      <mvc:resources mapping="/extjs/**" location="/WEB-INF/pages/extjs/" />  

Now create a controller class in the base package declared above. Include the mapping for your main page and json file as follows:

      public String userGrid() {  
           return "user_grid";  
      public ModelAndView showUserJson() {  
 //          Here you can also write code to fetch data from a database and add it  
 //          As a model object to view in form of json string, the view can then   
 //          display the data. But for convenience I have hard-coded the json data  
 //          in my pages/showJson.jsp file which I am returning here as a view  
           return new ModelAndView("showJson");  

Now write the file that will act as view to display the data (pages/user_grid.jsp)

 <%@ page language="java" contentType="text/html; charset=ISO-8859-1"  
 <!DOCTYPE html>  
 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">  
 <link rel="stylesheet" type="text/css" href="extjs/resources/css/ext-all.css" />  
 <script type="text/javascript" src="extjs/ext-all.js"></script>  
 <script type="text/javascript" src="extjs/ext-all-debug.js"></script>  
 Ext.define('User', {  
   extend: '',  
   fields: [{  
     name: 'id',  
     type: 'int'  
   }, {  
     name: 'name',  
     type: 'string'  
   }, {  
     name: 'email',  
     type: 'string'  
 Ext.onReady(function() {  
   var user = Ext.create('', {  
     storeId: 'user',  
     model: 'User',  
     autoLoad: 'true',  
     proxy: {  
       type: 'ajax',  
       url: 'user.json',  
       reader: {  
         type: 'json',  
         root: 'blah'  
   Ext.create('Ext.grid.Panel', {  
     store: user,  
     id: 'user',  
     title: 'Users',  
     columns: [{  
       header: 'ID',  
       dataIndex: 'id'  
     }, {  
       header: 'NAME',  
       dataIndex: 'name'  
     }, {  
       header: 'Email',  
       dataIndex: 'email'  
     height: 300,  
     width: 300,  
     renderTo: Ext.getBody()  

You also need to define a file called (pages/showJson.jsp) This file is model for our client ExtJs and a view for our Spring framework:

 { "blah":   
     "id": 1,  
     "name": "Abhas",  
     "email": ""  
     "id": 2,  
     "name": "Keshav",  
     "email": ""  

That’s it.. If you have followed all steps correctly you’ll get following result on running the project on server.

Ext js spring mvc grid panel
Hope you enjoyed reading this tutorial. Special thanks to Munawar sir (my mentor at internship) & Keshav Sharma (Man who can delete errors from your java code) for inspiring me to write this.