Click on the links below for Sencha Touch 2 List examples
List with itemtap event capture
List with Group Headings
List with IndexBar for easy Navigation
List with Disclosure Icon
List with buttons such as Add, Delete, Edit, etc.
List with Load More option at the page bottom.
Back-end programming for the above listed examples are provided below.
Source for WEB application config file - web.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | <? xml version = "1.0" encoding = "UTF-8" ?> < web-app id = "WebApp_ID" version = "2.4" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee < display-name >Sencha_DataView</ display-name > < servlet > < description > </ description > < display-name >CountryServlet</ display-name > < servlet-name >CountryServlet</ servlet-name > < servlet-class >com.as400samplecode.CountryServlet</ servlet-class > </ servlet > < servlet-mapping > < servlet-name >CountryServlet</ servlet-name > < url-pattern >/CountryServlet</ url-pattern > </ servlet-mapping > < welcome-file-list > < welcome-file >index.html</ welcome-file > < welcome-file >index.htm</ welcome-file > < welcome-file >index.jsp</ welcome-file > < welcome-file >default.html</ welcome-file > < welcome-file >default.htm</ welcome-file > < welcome-file >default.jsp</ welcome-file > </ welcome-file-list > </ web-app > |
Source for MySQL database resource - context.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | <? xml version = "1.0" encoding = "UTF-8" ?> < Context reloadable = "true" > < Resource auth = "Container" name = "jdbc/mysql" type = "javax.sql.DataSource" driverClassName = "com.mysql.jdbc.Driver" username = "root" password = "mysql" maxIdle = "10" maxActive = "200" maxWait = "5" removeAbandoned = "true" removeAbandonedTimeout = "1200" /> </ Context > |
Source for Country object - Country.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | package com.as400samplecode.util; public class Country { String code = null ; String name = null ; String continent = null ; String region = null ; Double lifeExpectancy = null ; Double gnp = null ; boolean disclosure = false ; public String getCode() { return code; } public void setCode(String code) { this .code = code; } public String getName() { return name; } public void setName(String name) { this .name = name; } public String getContinent() { return continent; } public void setContinent(String continent) { this .continent = continent; } public String getRegion() { return region; } public void setRegion(String region) { this .region = region; } public Double getLifeExpectancy() { return lifeExpectancy; } public void setLifeExpectancy(Double lifeExpectancy) { this .lifeExpectancy = lifeExpectancy; } public Double getGnp() { return gnp; } public void setGnp(Double gnp) { this .gnp = gnp; } public boolean isDisclosure() { return disclosure; } public void setDisclosure( boolean disclosure) { this .disclosure = disclosure; } } |
Java utility function to process data request - CountryInformation.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 | package com.as400samplecode.util; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.PreparedStatement; import java.util.ArrayList; import javax.naming.Context; import javax.naming.InitialContext; import javax.sql.DataSource; public class CountryInformation { Connection conn = null ; PreparedStatement stmt = null ; String sql = null ; public ArrayList<Country> getItems(String start, String limit) { ArrayList<Country> countryList = new ArrayList<Country>(); boolean toggle = false ; try { Context ctx = (Context) new InitialContext().lookup( "java:comp/env" ); conn = ((DataSource) ctx.lookup( "jdbc/mysql" )).getConnection(); sql = "Select * from COUNTRY order by name,code LIMIT ?,?" ; stmt = conn.prepareStatement(sql); stmt.setInt( 1 ,Integer.parseInt(start)); stmt.setInt( 2 ,Integer.parseInt(limit)); ResultSet rs = stmt.executeQuery(); while (rs.next()){ Country country = new Country(); country.setCode(rs.getString( "code" ).trim()); country.setName(rs.getString( "name" ).trim()); country.setContinent(rs.getString( "continent" ).trim()); country.setRegion(rs.getString( "region" ).trim()); country.setLifeExpectancy( rs.getString( "lifeExpectancy" ) == null ? new Double( 0 ) : Double.parseDouble(rs.getString( "lifeExpectancy" ) .trim())); country.setGnp( rs.getString( "gnp" ) == null ? new Double( 0 ) : Double.parseDouble(rs.getString( "gnp" ).trim())); toggle = toggle ? false : true ; country.setDisclosure(toggle); countryList.add(country); } rs.close(); stmt.close(); stmt = null ; conn.close(); conn = null ; } catch (Exception e){System.out.println(e);} finally { if (stmt != null ) { try { stmt.close(); } catch (SQLException sqlex) { // ignore -- as we can't do anything about it here } stmt = null ; } if (conn != null ) { try { conn.close(); } catch (SQLException sqlex) { // ignore -- as we can't do anything about it here } conn = null ; } } return countryList; } public int getTotalCount() { int totalCount = 0 ; try { Context ctx = (Context) new InitialContext().lookup( "java:comp/env" ); conn = ((DataSource) ctx.lookup( "jdbc/mysql" )).getConnection(); sql = "Select count(*) from COUNTRY" ; stmt = conn.prepareStatement(sql); ResultSet rs = stmt.executeQuery(); while (rs.next()){ totalCount = rs.getInt( 1 ); break ; } rs.close(); stmt.close(); stmt = null ; conn.close(); conn = null ; } catch (Exception e){System.out.println(e);} finally { /* * close any jdbc instances here that weren't * explicitly closed during normal code path, so * that we don't 'leak' resources... */ if (stmt != null ) { try { stmt.close(); } catch (SQLException sqlex) { // ignore -- as we can't do anything about it here } stmt = null ; } if (conn != null ) { try { conn.close(); } catch (SQLException sqlex) { // ignore -- as we can't do anything about it here } conn = null ; } } return totalCount; } } |
Java Servlet sending JSON response - CountryServlet.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | package com.as400samplecode; import java.io.IOException; import java.io.PrintWriter; import java.util.ArrayList; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.as400samplecode.util.Country; import com.as400samplecode.util.CountryInformation; import com.google.gson.Gson; import com.google.gson.JsonObject; public class CountryServlet extends HttpServlet { private static final long serialVersionUID = 1L; public CountryServlet() { super (); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request,response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String start = request.getParameter( "start" ); String limit = request.getParameter( "limit" ); PrintWriter out = response.getWriter(); response.setContentType( "text/html" ); CountryInformation countryInformation = new CountryInformation(); ArrayList<Country> countryList = countryInformation.getItems(start,limit); Gson gson = new Gson(); JsonObject myObj = new JsonObject(); myObj.addProperty( "success" , true ); myObj.add( "countries" , gson.toJsonTree(countryList)); myObj.addProperty( "totalCount" , countryInformation.getTotalCount()); out.println(myObj.toString()); out.close(); } } |
No comments:
Post a Comment
NO JUNK, Please try to keep this clean and related to the topic at hand.
Comments are for users to ask questions, collaborate or improve on existing.