In this tutorial session we will create simple application to load the drop down list options dynamically from the server side. Also we will load one\u00a0drop down list based on the selection of another\u00a0drop down list.<\/p>\n
We use AJAX, JQuery, Struts2 and json methodologies to achieve this. Also we assume the basic setup for struts2 is already ready. (i.e) All necessary jar files placed in right place. If you want more info please refer the Struts 2 Hello World application<\/a>.<\/p>\n Create a simple html file with the below content.<\/p>\n Create struts.xml file with the below content and place it under class folder root path.<\/p>\n As we are sending and receiving the data as json object we need to enable the json option in the struts.xml file. So implement a custom interceptor and json result type as above. (To do this\u00a0struts2-json-plugin.jar is required)<\/p>\n See our\u00a0web.xml file below.<\/p>\n Also create the java class ReadData which contains the business logic with the below code and place under com.rajesh.struts2 package in the class folder.<\/p>\n See the complete structure of the folder below.<\/p>\n <\/a><\/p>\n That’s all. Simply run the program as below and see the result\u00a0in action.<\/p>\n http:\/\/localhost:8080\/DynamicData\/<\/p>\n <\/a><\/p>\n For the above example I have created a complete .war file with source code. Download and just place under Apache web apps folder and run.<\/p>\n If you face any issues while execution please post in comments or send me email to rajeshmepco@gmail.com<\/em><\/p>\n","protected":false},"excerpt":{"rendered":" In this tutorial session we will create simple application to load the drop down list options dynamically from the server side. Also we will load one\u00a0drop down list based on the selection of another\u00a0drop down list. We use AJAX, JQuery, … Continue reading <!DOCTYPE HTML PUBLIC \"-\/\/W3C\/\/DTD HTML 4.0 Transitional\/\/EN\">\r\n<html>\r\n\r\n<head>\r\n <script src=\"js\/jquery-1.11.1.min.js\"><\/script>\r\n <script>\r\n $(function () {\r\n $('#state').html('');\r\n $.getJSON(\"readStates\", function (res) {\r\n for (var i = 0; i < res.states.length; i++) {\r\n $('#state').append(\r\n '<option value=' + res.states[i] + '>' + res.states[i] + '<\/option>');\r\n }\r\n });\r\n\r\n $(\"#state\").change(\r\n function () {\r\n $('#district').html('');\r\n var state = {\r\n \"state\": $(\"#state\").val()\r\n };\r\n $.ajax({\r\n url: \"readDistricts\",\r\n data: JSON.stringify(state),\r\n dataType: 'json',\r\n contentType: 'application\/json',\r\n type: 'POST',\r\n async: true,\r\n success: function (res) {\r\n console.log(res.districts.length);\r\n for (var i = 0; i < res.districts.length; i++) {\r\n console.log(\" \" + res.districts[i]);\r\n $('#district').append(\r\n '<option value=' + res.districts[i] + '>' + res.districts[i] + '<\/option>');\r\n }\r\n }\r\n });\r\n });\r\n });\r\n <\/script>\r\n<\/head>\r\n\r\n<body>\r\n <h3>Struts 2 Dynamic Drop down List<\/h3>\r\n State :\r\n <select id=\"state\"><\/select>District :\r\n <select id=\"district\"><\/select>\r\n<\/body>\r\n\r\n<\/html>\r\n<\/pre>\n
<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\r\n<!DOCTYPE struts PUBLIC\r\n \"-\/\/Apache Software Foundation\/\/DTD Struts Configuration 2.0\/\/EN\"\r\n \"http:\/\/struts.apache.org\/dtds\/struts-2.0.dtd\">\r\n<struts>\r\n <package name=\"default\" namespace=\"\/\" extends=\"json-default\">\r\n <interceptors>\r\n <interceptor-stack name=\"defaultStack\">\r\n <interceptor-ref name=\"json\">\r\n <param name=\"enableSMD\">true<\/param>\r\n <\/interceptor-ref>\r\n <\/interceptor-stack>\r\n <\/interceptors>\r\n <default-interceptor-ref name=\"defaultStack\" \/>\r\n\r\n <action name=\"readDistricts\" class=\"com.rajesh.struts2.ReadData\" method=\"dbDistricts\">\r\n <result type=\"json\"><\/result>\r\n <\/action>\r\n\r\n <action name=\"readStates\" class=\"com.rajesh.struts2.ReadData\" method=\"dbStates\">\r\n <result type=\"json\"><\/result>\r\n <\/action>\r\n\r\n <\/package>\r\n<\/struts><\/pre>\n
<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n<web-app xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\"\r\n\txmlns=\"http:\/\/java.sun.com\/xml\/ns\/javaee\"\r\n\txsi:schemaLocation=\"http:\/\/java.sun.com\/xml\/ns\/javaee http:\/\/java.sun.com\/xml\/ns\/javaee\/web-app_3_0.xsd\"\r\n\tid=\"WebApp_ID\" version=\"3.0\">\r\n\t<display-name>HelloStruts<\/display-name>\r\n\r\n\t<filter>\r\n\t\t<filter-name>struts2<\/filter-name>\r\n\t\t<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter<\/filter-class>\r\n\t<\/filter>\r\n\r\n\t<filter-mapping>\r\n\t\t<filter-name>struts2<\/filter-name>\r\n\t\t<url-pattern>\/*<\/url-pattern>\r\n\t<\/filter-mapping>\r\n\r\n\t<welcome-file-list>\r\n\t\t<welcome-file>\/index.html<\/welcome-file>\r\n\t<\/welcome-file-list>\r\n<\/web-app><\/pre>\n
package com.rajesh.struts2;\r\n\r\nimport com.opensymphony.xwork2.ActionSupport;\r\n\r\npublic class ReadData extends ActionSupport {\r\n\r\n private static final long serialVersionUID = -8819352697303500472L;\r\n\r\n private String state, district;\r\n private String states[], districts[];\r\n\r\n public String[] getDistricts() {\r\n return districts;\r\n }\r\n\r\n public void setDistricts(String[] districts) {\r\n this.districts = districts;\r\n }\r\n\r\n public String[] getStates() {\r\n return states;\r\n }\r\n\r\n public void setStates(String[] states) {\r\n this.states = states;\r\n }\r\n\r\n public String getState() {\r\n return state;\r\n }\r\n\r\n public void setState(String state) {\r\n System.out.println(\"Inside Setter \" + state);\r\n this.state = state;\r\n }\r\n\r\n public String getDistrict() {\r\n return district;\r\n }\r\n\r\n public void setDistrict(String district) {\r\n this.district = district;\r\n }\r\n\r\n public String dbDistricts() {\r\n System.out.println(\"Getting Districts for \" + state);\r\n \/\/Do the database code or business logic here.\r\n districts = new String[5];\r\n if (state.equalsIgnoreCase(\"tamilnadu\")) {\r\n districts[0] = \"chennai\";\r\n districts[1] = \"madurai\";\r\n districts[2] = \"trichy\";\r\n districts[3] = \"Covai\";\r\n districts[4] = \"Pudukkottai\";\r\n } else if (state.equalsIgnoreCase(\"kerala\")) {\r\n districts[0] = \"allappey\";\r\n districts[1] = \"trivandrum\";\r\n districts[2] = \"kozhikkode\";\r\n districts[3] = \"District 4\";\r\n districts[4] = \"District 5\";\r\n } else if (state.equalsIgnoreCase(\"karnataka\")) {\r\n districts[0] = \"bangalore\";\r\n districts[1] = \"Bommanahalli\";\r\n districts[2] = \"Mysore\";\r\n districts[3] = \"District 4\";\r\n districts[4] = \"District 5\";\r\n } else {\r\n districts[0] = \"District 1\";\r\n districts[1] = \"District 2\";\r\n districts[2] = \"District 3\";\r\n districts[3] = \"District 4\";\r\n districts[4] = \"District 5\";\r\n }\r\n return SUCCESS;\r\n }\r\n\r\n public String dbStates() {\r\n \/\/Do the database code or business logic here.\r\n states = new String[5];\r\n states[0] = \"tamilnadu\";\r\n states[1] = \"kerala\";\r\n states[2] = \"karnataka\";\r\n states[3] = \"delhi\";\r\n states[4] = \"kashmir\";\r\n return SUCCESS;\r\n }\r\n}<\/pre>\n
Dynamic Dropdown List<\/a><\/h3>\n