Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Spring MVC: Integrate Sencha Touch List

DZone's Guide to

Spring MVC: Integrate Sencha Touch List

· Java Zone
Free Resource

Microservices! They are everywhere, or at least, the term is. When should you use a microservice architecture? What factors should be considered when making that decision? Do the benefits outweigh the costs? Why is everyone so excited about them, anyway?  Brought to you in partnership with IBM.

Most important and useful component in mobile application is List. In this tutorial you will learn how to create Sencha Touch List view with Spring MVC 3 in backend. For this tutorial I am going to create a List of movies with other details using Sencha Touch, it’s very basic and easy tutorial and I am sure you will enjoy.

First of all, create an eclipse project; add Spring MVC and Sencha Touch dependencies. Following is a screenshot of my eclipse project.

I am using Spring MVC with XML free approach. If you are new to Spring MVC and don’t about XML free configuration, please read my previous post on XML free Spring MVC.

Create a simple movie POJO.


public class Movie {

	private int id;
	private String title, director;
	private int yearOfRelease;
	private MovieGenre genre;

	public enum MovieGenre {
		Action, Comedy, Family, Horror, War;
	}

	public Movie(){}

	public Movie(int id, String title, String director, 
			int yearOfRelease, MovieGenre genre)
	{
		super();
		this.id = id;
		this.title = title;
		this.director = director;
		this.yearOfRelease = yearOfRelease;
		this.genre = genre;
	}

	//getter, settings method
}

Now create a class which has multiple movie objects that we need to populate in a List.

package org.techzoo.touch.vo;

import static org.techzoo.touch.vo.Movie.MovieGenre.Action;
import static org.techzoo.touch.vo.Movie.MovieGenre.Comedy;
import static org.techzoo.touch.vo.Movie.MovieGenre.Family;
import static org.techzoo.touch.vo.Movie.MovieGenre.Horror;
import static org.techzoo.touch.vo.Movie.MovieGenre.War;

import java.util.ArrayList;
import java.util.List;

public class MoviesDatabase {

	private List movies = null;

	private static MoviesDatabase _instance = null;

	private MoviesDatabase() {
		if(null == movies){
			initializeMovies();
		}
	}

	public static MoviesDatabase getInstance() {
		if(null == _instance){
			return new MoviesDatabase();
		}
		return _instance;
	}

	public List getMovies() {
		return movies;
	}

	private void initializeMovies() {
		movies = new ArrayList();

		/** Action **/
		movies.add(new Movie(1001, "Ek Tha Tiger", "Kabir Khan", 2012, Action));
		movies.add(new Movie(1002, "Khiladi", "Abbas Mastan", 1992, Action));
		movies.add(new Movie(1003, "Kick", "Sajid Nadiadwala", 2014, Action));
		movies.add(new Movie(1004, "Rowdy Rathore", "Prabhudheva", 2012, Action));

		/** Horror **/
		movies.add(new Movie(1005, "Bhoot", "Ram Gopal Varma", 2003, Horror));
		movies.add(new Movie(1006, "Raat", "Ram Gopal Varma", 1992, Horror));
		movies.add(new Movie(1007, "Aatma", "Deepak Ramsay", 2013, Horror));
		movies.add(new Movie(1008, "Mahal", "Kamal Amrohi", 1949, Horror));

		/** Comedy **/
		movies.add(new Movie(1009, "Gol Maal", "Hrishikesh Mukherjee", 1979, Comedy));
		movies.add(new Movie(1010, "Jaane bhi do Yaaro", "Kundan Shah", 1983, Comedy));
		movies.add(new Movie(1011, "Andaz apna apna", "Rajkumar Santoshi", 1994, Comedy));
		movies.add(new Movie(1012, "3 Idiots", "Rajkumar Hirani", 2009, Comedy));

		/** Family **/
		movies.add(new Movie(1013, "Chakde India", "Shimit Amin", 2007, Family));
		movies.add(new Movie(1014, "Khichdi", "Aatish Kapadia", 2010, Family));
		movies.add(new Movie(1015, "Vivah", "Sooraj Barjatya", 2006, Family));
		movies.add(new Movie(1016, "Beta", "Indra Kumar", 1992, Family));

		/** War **/
		movies.add(new Movie(1017, "Border", "JP Datta", 1997, War));
		movies.add(new Movie(1018, "LOC Kargil", "JP Datta", 2003, War));
		movies.add(new Movie(1019, "Lakshya", "Farhan Akhtar", 2004, War));
		movies.add(new Movie(1020, "Tango Cahrlie", "Mani Shankar", 2005, War));
	}

}

Now create a controller which will load all the movies from server to list.

package org.techzoo.touch.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.techzoo.touch.vo.Movie;
import org.techzoo.touch.vo.MoviesDatabase;

@Controller
@RequestMapping("movies")
public class MovieController {

	@RequestMapping(value = "/index")
	public String index()
	{
		return "index";
	}

	@RequestMapping (value = "/load.do", method = RequestMethod.GET)
	public @ResponseBody Map loadMovies()
	{
		MoviesDatabase mdb = MoviesDatabase.getInstance();
		List movies = mdb.getMovies();
		Map resp = new HashMap();
		resp.put("moviesList", movies);
		return resp;
	}

}

Now we are done from backend and lts time to create a Touch ListView and Store.

Ext.define('TechZoo.view.MovieList', {
	extend	: 'Ext.dataview.List',
    alias 	: 'widget.movielist',
    config	: {
    	store 	: 'Movie',
    	grouped	: true,
    	indexBar: true,
    	itemTpl : '{title}',
    	listeners : {
        	itemtap : function(me, index, target, record){
        		Ext.Msg.alert('Movie Detail', 
        			'Director: ' + record.get('director') + 
        			'Year of Release : ' + record.get('yearOfRelease'));	
        	}
        },
    	onItemDisclosure : true
    }
});

Our List also require store. Store use to load data from server/memory by using proxy.

Ext.define('TechZoo.store.Movie', {
    extend    : 'Ext.data.Store',
    storeId    : 'movieStore',
    config    : {
        model    : 'TechZoo.model.Movie',
        autoLoad: true,
        sorters    : 'genre',
        grouper    : {
            groupFn    : function(record) {
                return record.get('genre');
            },
            sortProperty    : 'genre'
        },
        proxy    : {
            type: 'ajax',
            url    : 'movies/load.do',
            reader    : {
                type    : 'json',
                rootProperty: 'moviesList'
            }
        }
    }
});
Ext.create('TechZoo.store.Movie');

Ok, so now we are all set. Publish your project and hit the URL in browser (Safari or Chrome) and you will see following result.

Click on any List Item and you will see a PopUp with movie details as below.


Discover how the Watson team is further developing SDKs in Java, Node.js, Python, iOS, and Android to access these services and make programming easy. Brought to you in partnership with IBM.

Topics:

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}