Deprecated: Creation of dynamic property wpdb::$categories is deprecated in /home2/learnera/public_html/tech/wp-includes/wp-db.php on line 760

Deprecated: Creation of dynamic property wpdb::$post2cat is deprecated in /home2/learnera/public_html/tech/wp-includes/wp-db.php on line 760

Deprecated: Creation of dynamic property wpdb::$link2cat is deprecated in /home2/learnera/public_html/tech/wp-includes/wp-db.php on line 760

Deprecated: Using ${var} in strings is deprecated, use {$var} instead in /home2/learnera/public_html/tech/wp-includes/comment-template.php on line 1747

Deprecated: Creation of dynamic property WPtouchProFour::$settings_object is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/wptouch/core/class-wptouch-pro.php on line 82

Deprecated: Automatic conversion of false to array is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/wptouch/core/admin-load.php on line 70

Deprecated: Creation of dynamic property WP_Block_Type::$skip_inner_blocks is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-block-type.php on line 391

Deprecated: Creation of dynamic property WP_Block_Type::$skip_inner_blocks is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-block-type.php on line 391

Deprecated: Creation of dynamic property Advanced_Editor_Tools::$toolbar_classic_block is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/tinymce-advanced/tinymce-advanced.php on line 347

Deprecated: Creation of dynamic property Advanced_Editor_Tools::$toolbar_block is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/tinymce-advanced/tinymce-advanced.php on line 349

Deprecated: Creation of dynamic property Advanced_Editor_Tools::$toolbar_block_side is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/tinymce-advanced/tinymce-advanced.php on line 350

Deprecated: Creation of dynamic property Advanced_Editor_Tools::$panels_block is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/tinymce-advanced/tinymce-advanced.php on line 351

Deprecated: Creation of dynamic property Advanced_Editor_Tools::$used_block_buttons is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/tinymce-advanced/tinymce-advanced.php on line 354

Deprecated: Creation of dynamic property YARPP::$is_custom_template is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/yet-another-related-posts-plugin/classes/YARPP_Core.php on line 56

Deprecated: Creation of dynamic property YARPP::$db_options is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/yet-another-related-posts-plugin/classes/YARPP_Core.php on line 69

Deprecated: Creation of dynamic property WP_Term::$object_id is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-term-query.php on line 1118

Deprecated: Return type of Requests_Cookie_Jar::offsetExists($key) should either be compatible with ArrayAccess::offsetExists(mixed $offset): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home2/learnera/public_html/tech/wp-includes/Requests/Cookie/Jar.php on line 63

Deprecated: Return type of Requests_Cookie_Jar::offsetGet($key) should either be compatible with ArrayAccess::offsetGet(mixed $offset): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home2/learnera/public_html/tech/wp-includes/Requests/Cookie/Jar.php on line 73

Deprecated: Return type of Requests_Cookie_Jar::offsetSet($key, $value) should either be compatible with ArrayAccess::offsetSet(mixed $offset, mixed $value): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home2/learnera/public_html/tech/wp-includes/Requests/Cookie/Jar.php on line 89

Deprecated: Return type of Requests_Cookie_Jar::offsetUnset($key) should either be compatible with ArrayAccess::offsetUnset(mixed $offset): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home2/learnera/public_html/tech/wp-includes/Requests/Cookie/Jar.php on line 102

Deprecated: Return type of Requests_Cookie_Jar::getIterator() should either be compatible with IteratorAggregate::getIterator(): Traversable, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home2/learnera/public_html/tech/wp-includes/Requests/Cookie/Jar.php on line 111

Deprecated: Return type of Requests_Utility_CaseInsensitiveDictionary::offsetExists($key) should either be compatible with ArrayAccess::offsetExists(mixed $offset): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home2/learnera/public_html/tech/wp-includes/Requests/Utility/CaseInsensitiveDictionary.php on line 40

Deprecated: Return type of Requests_Utility_CaseInsensitiveDictionary::offsetGet($key) should either be compatible with ArrayAccess::offsetGet(mixed $offset): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home2/learnera/public_html/tech/wp-includes/Requests/Utility/CaseInsensitiveDictionary.php on line 51

Deprecated: Return type of Requests_Utility_CaseInsensitiveDictionary::offsetSet($key, $value) should either be compatible with ArrayAccess::offsetSet(mixed $offset, mixed $value): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home2/learnera/public_html/tech/wp-includes/Requests/Utility/CaseInsensitiveDictionary.php on line 68

Deprecated: Return type of Requests_Utility_CaseInsensitiveDictionary::offsetUnset($key) should either be compatible with ArrayAccess::offsetUnset(mixed $offset): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home2/learnera/public_html/tech/wp-includes/Requests/Utility/CaseInsensitiveDictionary.php on line 82

Deprecated: Return type of Requests_Utility_CaseInsensitiveDictionary::getIterator() should either be compatible with IteratorAggregate::getIterator(): Traversable, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home2/learnera/public_html/tech/wp-includes/Requests/Utility/CaseInsensitiveDictionary.php on line 91

Warning: Cannot modify header information - headers already sent by (output started at /home2/learnera/public_html/tech/wp-includes/wp-db.php:760) in /home2/learnera/public_html/tech/wp-includes/rest-api/class-wp-rest-server.php on line 1723

Warning: Cannot modify header information - headers already sent by (output started at /home2/learnera/public_html/tech/wp-includes/wp-db.php:760) in /home2/learnera/public_html/tech/wp-includes/rest-api/class-wp-rest-server.php on line 1723

Warning: Cannot modify header information - headers already sent by (output started at /home2/learnera/public_html/tech/wp-includes/wp-db.php:760) in /home2/learnera/public_html/tech/wp-includes/rest-api/class-wp-rest-server.php on line 1723

Warning: Cannot modify header information - headers already sent by (output started at /home2/learnera/public_html/tech/wp-includes/wp-db.php:760) in /home2/learnera/public_html/tech/wp-includes/rest-api/class-wp-rest-server.php on line 1723

Warning: Cannot modify header information - headers already sent by (output started at /home2/learnera/public_html/tech/wp-includes/wp-db.php:760) in /home2/learnera/public_html/tech/wp-includes/rest-api/class-wp-rest-server.php on line 1723

Warning: Cannot modify header information - headers already sent by (output started at /home2/learnera/public_html/tech/wp-includes/wp-db.php:760) in /home2/learnera/public_html/tech/wp-includes/rest-api/class-wp-rest-server.php on line 1723

Deprecated: Creation of dynamic property WPDM\Package\Package::$post_author is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$post_date is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$post_date_gmt is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$post_content is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$post_title is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$post_excerpt is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$comment_status is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$ping_status is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$post_password is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$post_name is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$to_ping is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$pinged is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$post_modified is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$post_modified_gmt is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$post_content_filtered is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$post_parent is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$guid is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$menu_order is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$post_type is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$post_mime_type is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$comment_count is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$filter is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$author_name is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$author_profile_url is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$avatar_url is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$avatar is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$thumb is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$preview is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$file_count is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$page_url is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$page_link is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$page_url_qr is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$btnclass is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$file_size is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$file_type_icon is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$download_url is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$download_link is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$download_link_extended is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$download_link_popup is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$formatted is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$__template is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Deprecated: Creation of dynamic property WPDM\Package\Package::$__template_type is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/download-manager/src/Package/PackageController.php on line 375

Warning: Cannot modify header information - headers already sent by (output started at /home2/learnera/public_html/tech/wp-includes/wp-db.php:760) in /home2/learnera/public_html/tech/wp-includes/rest-api/class-wp-rest-server.php on line 1723

Warning: Cannot modify header information - headers already sent by (output started at /home2/learnera/public_html/tech/wp-includes/wp-db.php:760) in /home2/learnera/public_html/tech/wp-includes/rest-api/class-wp-rest-server.php on line 1723
{"id":279,"date":"2014-05-17T12:59:30","date_gmt":"2014-05-17T12:59:30","guid":{"rendered":"http:\/\/tech.learnerandtutor.com\/?p=279"},"modified":"2014-12-06T06:18:31","modified_gmt":"2014-12-06T06:18:31","slug":"dynamic-drop-down-list-with-struts2-ui-tag-without-ajax-jquery","status":"publish","type":"post","link":"https:\/\/tech.learnerandtutor.com\/dynamic-drop-down-list-with-struts2-ui-tag-without-ajax-jquery\/","title":{"rendered":"Dynamic Drop Down List with Struts2 UI tag without AJAX, jQuery"},"content":{"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.<\/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

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

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

<?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

See our struts.xml below.<\/p>\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

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

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

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

<s:select label=\"What's your State\" headerKey=\"-1\" \r\nheaderValue=\"Select State\" list=\"states\" name=\"state\" value=\"defaultState\" \/><\/pre>\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

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

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

<s:select label=\"What's your State\" headerKey=\"-1\" \r\nheaderValue=\"Select State\" list=\"states\" name=\"state\" value=\"defaultState\" \/><\/pre>\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.
\n\"struts2UI_dropdown\"<\/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

<\/p>\n