In this tutorial session we will create simple application to load the drop down list options dynamically from the server side. Also we will use only struts 2 UI tag, not ajax or jquery.<\/p>\n
In our previous example<\/a> we used ajax call to load the drop down options from server side via jquery. Here not.<\/p>\n We assume that we already have a very basic knowledge over struts2 setup. If not please refer our Hello World application<\/a>. To achieve our goal first create a simple jsp file as below.<\/p>\n Then in web.xml divert all requests to struts2 framework as usual. See the web.xml below.<\/p>\n Also please note that we never mentioned the welcome file list in our web.xml. (i.e) Home page. We will handle that in our struts.xml.<\/p>\n See our struts.xml below.<\/p>\n What is the change we made in our struts.xml. We mapped a empty action to a class and if the result is success then we are rendering the page home.jsp to the browser. Why instead of mentioning the home page in web.xml we mentioned in struts.xml? There is a reason behind that. We will discuss it later.<\/p>\n Now see the corresponding action class ReadData.java below.<\/p>\n That’s all the setup. But we need to know why we rendered the home page via struts.xml? Also the empty action pointed to the method dbStates() is not doing anything. It simply returns a\u00a0“success”<\/span>\u00a0 string. Then why?<\/p>\n The answer is here. In our home page home.jsp we used struts2 tag for the drop down list. See the code<\/p>\n So if we render the page directly from web.xml it don’t have any values for the drop down list options. We need to run some code before rendering the page. Right? That’s why we diverted the empty action to ReadData.java<\/span> . See the constructor of the ReadData.java<\/span> .<\/p>\n So when we point to this class it will automatically creates an arraylist and fills it with the required value. So after this constructor executed the control will go to the dbStates()<\/span>\u00a0method. There we simply returns a “success” string. So the control will come back to struts.xml. There we mentioned <result name=”success”>home.jsp<\/result><\/span> . So it will render the page home.jsp to the browser.<\/p>\n But the difference is while render the page home.jsp. When it comes to the Struts UI tag<\/p>\n it will look for method\u00a0getStat()<\/span> in the action class. Because we mentioned the name as state. So as per struts 2 design pattern it will try to run a method by name getStat()<\/span> .<\/p>\n As we have that method in our action class it will get executed. Then with the return value as arraylist it will be filled inside the drop down list by the framework and rendered to the browser .<\/p>\n Simply run the code as below http:\/\/localhost:8080\/DynamicData\/ <\/em>and see the result in action. 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 use only struts 2 UI tag, not ajax or jquery. In our previous example we used … Continue reading home.jsp<\/h4>\n
<%@ taglib prefix=\"s\" uri=\"\/struts-tags\" %>\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\r\n \/\/We commented this code because we are directly loading the drop down options via struts UI tag\r\n \/*$('#state').html('');\r\n\t\t$.getJSON(\"readStates\", function(res) {\r\n\t\t\tfor ( var i = 0; i < res.states.length; i++) {\r\n\t\t\t\t$('#state').append(\r\n\t\t\t\t\t\t'<option value='+res.states[i]+'>' + res.states[i]\r\n\t\t\t\t\t\t\t\t+ '<\/option>');\r\n\t\t\t}\r\n\t\t});*\/\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 <s:select label=\"What's your State\" headerKey=\"-1\" headerValue=\"Select State\" list=\"states\" name=\"state\" value=\"defaultState\" \/>\r\nDistrict :<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<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>DynamicData<\/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<\/web-app><\/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=\"\" class=\"com.rajesh.struts2.ReadData\" method=\"dbStates\">\r\n <result name=\"success\">home.jsp<\/result>\r\n <\/action>\r\n\r\n <\/package>\r\n<\/struts><\/pre>\n
ReadData.java<\/h4>\n
package com.rajesh.struts2;\r\n\r\nimport java.util.ArrayList;\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 private String state;\r\n private ArrayList < String > districts;\r\n private ArrayList < String > states;\r\n\r\n public ReadData() {\r\n System.out.println(\"Inside Constructor \");\r\n states = new ArrayList < String > ();\r\n states.add(\"tamilnadu\");\r\n states.add(\"kerala\");\r\n states.add(\"karnataka\");\r\n states.add(\"delhi\");\r\n }\r\n\r\n public String getDefaultState() {\r\n System.out.println(\"Inside default State Getter\");\r\n return \"Select State\";\r\n }\r\n\r\n public ArrayList < String > getDistricts() {\r\n return districts;\r\n }\r\n\r\n public ArrayList < String > getStates() {\r\n return states;\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 dbDistricts() {\r\n System.out.println(\"Getting Districts for \" + state);\r\n \/\/ Do the database code or business logic here.\r\n districts = new ArrayList < String > ();\r\n if (state.equalsIgnoreCase(\"tamilnadu\")) {\r\n districts.add(\"chennai\");\r\n districts.add(\"madurai\");\r\n districts.add(\"trichy\");\r\n districts.add(\"Covai\");\r\n districts.add(\"Pudukkottai\");\r\n } else if (state.equalsIgnoreCase(\"kerala\")) {\r\n districts.add(\"allappey\");\r\n districts.add(\"trivandrum\");\r\n districts.add(\"kozhikkode\");\r\n } else if (state.equalsIgnoreCase(\"karnataka\")) {\r\n districts.add(\"bangalore\");\r\n districts.add(\"Bommanahalli\");\r\n districts.add(\"Mysore\");\r\n } else if (state.equalsIgnoreCase(\"-1\")) {} else {\r\n districts.add(\"District 1\");\r\n districts.add(\"District 2\");\r\n districts.add(\"District 3\");\r\n districts.add(\"District 4\");\r\n districts.add(\"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 System.out.println(\"Inside dbStates method\");\r\n return SUCCESS;\r\n }\r\n}<\/pre>\n
<s:select label=\"What's your State\" headerKey=\"-1\" \r\nheaderValue=\"Select State\" list=\"states\" name=\"state\" value=\"defaultState\" \/><\/pre>\n
public ReadData() {\r\n System.out.println(\"Inside Constructor \");\r\n states = new ArrayList < String > ();\r\n states.add(\"tamilnadu\");\r\n states.add(\"kerala\");\r\n states.add(\"karnataka\");\r\n states.add(\"delhi\");\r\n}<\/pre>\n
<s:select label=\"What's your State\" headerKey=\"-1\" \r\nheaderValue=\"Select State\" list=\"states\" name=\"state\" value=\"defaultState\" \/><\/pre>\n
\n<\/a><\/p>\nDynamic Dropdown List<\/a><\/h3>\n