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: 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":404,"date":"2014-05-27T11:38:48","date_gmt":"2014-05-27T11:38:48","guid":{"rendered":"http:\/\/tech.learnerandtutor.com\/?p=404"},"modified":"2014-12-06T06:16:02","modified_gmt":"2014-12-06T06:16:02","slug":"struts2-tutorial-part-3-actions-2","status":"publish","type":"post","link":"http:\/\/tech.learnerandtutor.com\/struts2-tutorial-part-3-actions-2\/","title":{"rendered":"Struts2 Tutorial Part 3 \u2013 Actions 2"},"content":{"rendered":"

In this tutorial session we will create a simple struts 2 application to understand the complete flow of an action. If you want to know the clear picture about Struts2 action see our action post 1 here<\/a>. If you want to get clear idea over struts 2 from the beginning please refer the below posts.<\/p>\n

Struts2 Introduction<\/a>
\n
Struts2 HelloWorld Application<\/a>
\n
Struts2 Tutorial Part 3 \u2013 Actions 1<\/a><\/p>\n

While develop our application, we will discuss about the details of each configuration parameter. First we have to create the home.jsp file which is the first page of the application. See the code below.<\/p>\n

home.jsp<\/h4>\n
<%@ taglib prefix=\"s\" uri=\"\/struts-tags\" %>\r\n    <html>\r\n\r\n    <head>\r\n        <title>Struts 2 Custom URL<\/title>\r\n        <script src=\"jquery-1.11.1.min.js\"><\/script>\r\n    <\/head>\r\n\r\n    <body>\r\n        <h1>Struts 2 Action Tutorial<\/h1>\r\n        <s:form action=\"welcome\" method=\"post\" enctype=\"multipart\/form-data\">\r\n            <s:textfield key=\"Name\" name=\"name\" \/>\r\n            <s:textfield key=\"City\" name=\"city\" \/>\r\n            <s:submit key=\"submit\" \/>\r\n        <\/s:form>\r\n    <\/body>\r\n\r\n    <\/html><\/pre>\n

We created a simple jsp file with struts2 UI tags. We will learn about Struts2 UI tags later. As of now just keep in mind struts2 tags are like normal html tags with more functionalists. So for clear understanding consider\u00a0<s:form><\/span>\u00a0 as normal\u00a0<form><\/span>\u00a0 tag,\u00a0<s:textfield><\/span>\u00a0 as normal\u00a0<input type=’text’><\/span>\u00a0 tag and so.So when we fill and submit the form it will submit the data to the url welcome.<\/p>\n

Next we will create another jsp page where we will display the name and city what we entered in the home.jsp. See the code below.<\/p>\n

welcome.jsp<\/h4>\n
<%@ taglib prefix=\"s\" uri=\"\/struts-tags\"%>\r\n<html>\r\n<head>\r\n    <title>Struts2 Custom URL<\/title>\r\n<\/head>\r\n\r\n<body>\r\n    <h2>\r\nWelcome to Struts2 Actions Tutorial\r\n<s:property value=\"name\" \/>,<s:property value=\"city\" \/>.\r\n<\/h2>\r\n    <a href=\"\/Struts2Action\">Go Back to Home<\/a>\r\n<\/body>\r\n\r\n<\/html><\/pre>\n

Here also we used struts2 UI\u00a0 tag <s:property><\/span>\u00a0. It is used to retrieve the value from the OGNL stack where all the variables are stored. We will see about the Struts2 UI tags and OGNL stack later. As of now assume it will return the respective values.<\/p>\n

Next we will create our struts.xml file which is the configuration file, See the code below.<\/p>\n

struts.xml<\/h4>\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=\"struts-default\">\r\n        <action name=\"\">\r\n            <result>home.jsp<\/result>\r\n        <\/action>\r\n        <action name=\"welcome\" class=\"com.rajesh.struts2.Welcome\" method=\"welcome\">\r\n            <result name=\"success\">welcome.jsp<\/result>\r\n            <result name=\"error\">error.jsp<\/result>\r\n        <\/action>\r\n    <\/package>\r\n<\/struts><\/pre>\n

See the web.xml file below.<\/p>\n

web.xml<\/h4>\n
<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\r\n<web-app xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\" xmlns=\"http:\/\/java.sun.com\/xml\/ns\/javaee\" xsi:schemaLocation=\"http:\/\/java.sun.com\/xml\/ns\/javaee http:\/\/java.sun.com\/xml\/ns\/javaee\/web-app_3_0.xsd\" id=\"WebApp_ID\" version=\"3.0\">\r\n    <display-name>Struts2Action<\/display-name>\r\n\r\n    <filter>\r\n        <filter-name>struts2<\/filter-name>\r\n        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter<\/filter-class>\r\n    <\/filter>\r\n\r\n    <filter-mapping>\r\n        <filter-name>struts2<\/filter-name>\r\n        <url-pattern>\/*<\/url-pattern>\r\n    <\/filter-mapping>\r\n\r\n<\/web-app><\/pre>\n

Finally see our java file where we have our business logic.<\/p>\n

Welcome<\/span>.java<\/h4>\n
package com.rajesh.struts2;\r\n\r\nimport com.opensymphony.xwork2.ActionSupport;\r\n\r\npublic class Welcome extends ActionSupport {\r\n\r\n    private static final long serialVersionUID = 1500617172067606799L;\r\n\r\n    private String name, city;\r\n\r\n    public String getName() {\r\n        return name;\r\n    }\r\n\r\n    public void setName(String name) {\r\n        this.name = name;\r\n    }\r\n\r\n    public String getCity() {\r\n        return city;\r\n    }\r\n\r\n    public void setCity(String city) {\r\n        this.city = city;\r\n    }\r\n\r\n    public String welcome() {\r\n        return SUCCESS;\r\n    }\r\n}<\/pre>\n

That’s all the files needs to be created. Now we will discuss the workflow of this application to understand actions clearly. At first when the user types\u00a0http:\/\/localhost:8080\/Struts2Action\/<\/span>\u00a0 in the browser the control will go the web.xml file as we discussed in the previous chapter. In web.xml we never mentioned any index files. But we mentioned a filter to all the incoming requests.<\/p>\n

<filter>\r\n    <filter-name>struts2<\/filter-name>\r\n    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter<\/filter-class>\r\n<\/filter><\/pre>\n

So the control will go to the filter class. We no need to worry abouth these portions. These are all implemented by the framework itself.\u00a0 Just for understanding I am explaining this.<\/p>\n

Then the framework will look for struts.xml file. Then as we typed simply the application name alone it will look for the action with emty name. In our struts.xml file we have a action with name emty. So the framework will render the page home.jsp to the browser.<\/p>\n

Here one important think we have to note is before sending the page to browser the framework will replace all struts2 UI tags by corresponding html tags. To understand this after the home.jsp is rendered on the browser see the source code of the page.<\/p>\n

After rendering the page the page source code will look like below one.<\/p>\n

<html>\r\n\r\n<head>\r\n    <title>Struts 2 Custom URL<\/title>\r\n<\/head>\r\n\r\n<body>\r\n    <h1>Struts 2 Action Tutorial<\/h1>\r\n    <form id=\"welcome\" name=\"welcome\" action=\"\/Struts2Action\/welcome.action\" method=\"post\" enctype=\"multipart\/form-data\">\r\n        <table class=\"wwFormTable\">\r\n            <tr>\r\n                <td class=\"tdLabel\">\r\n                    <label for=\"welcome_name\" class=\"label\">Name:<\/label>\r\n                <\/td>\r\n                <td>\r\n                    <input type=\"text\" name=\"name\" value=\"\" id=\"welcome_name\" \/>\r\n                <\/td>\r\n            <\/tr>\r\n            <tr>\r\n                <td class=\"tdLabel\">\r\n                    <label for=\"welcome_city\" class=\"label\">City:<\/label>\r\n                <\/td>\r\n                <td>\r\n                    <input type=\"text\" name=\"city\" value=\"\" id=\"welcome_city\" \/>\r\n                <\/td>\r\n            <\/tr>\r\n            <tr>\r\n                <td colspan=\"2\">\r\n                    <div align=\"right\">\r\n                        <input type=\"submit\" id=\"welcome_submit\" name=\"submit\" value=\"submit\" \/>\r\n                    <\/div>\r\n                <\/td>\r\n            <\/tr>\r\n\r\n        <\/table>\r\n    <\/form>\r\n<\/body>\r\n\r\n<\/html><\/pre>\n

Then\u00a0 as in the home page we have to enter the name,city then submit the form. As we mentioned the data to the url welcome.action the control will look for it. So the request will be http:\/\/localhost:8080\/Struts2Action\/welcome.action<\/span>\u00a0. As usual the control will look for an action with name ‘welcome’. Once it identifies it will execute the corresponding class to it.<\/p>\n

As we discussed all action methods should return a string value. Once that method returned a string value, the matching result tag will be rendered to the user. In our class com.rajesh.struts2.Welcome method welcome() we returned ‘success’ string. So welcome.jsp will be rendered to the user. If suppose our method returns string ‘error’ then the error.jsp will be rendered. If no name attribute is defined with the result tag ‘success’ will be taken as default value.<\/p>\n

Note:<\/h4>\n

In our previous action with empty name we never mentioned any class name. Just remember that the class attribute of the action is an optional one (Discussed in previous chapter<\/a>). If no class is specified it will take the result as ‘success’ by default<\/p>\n

Another attribute for the action tag is method name. See our action welcome.<\/p>\n

<action name=\"welcome\" class=\"com.rajesh.struts2.Welcome\" method=\"welcome\">\r\n<\/pre>\n

We mentioned the method name as welcome(). If we don’t specify the method name the control will look for method with name execute() inside the action class.<\/p>\n

What is the use of this?<\/h4>\n

The main reason is we can have multiple methods inside a single class. In that situation we can mention the method name, Otherwise we can simply use the same method name execute(). Anyway it is upto us to decide.<\/p>\n

With this knowledge we will look back our initial action with empty name “”. We never mentioned the class to it. So it will take ‘success’ by default. Then it will look for result tage with name ‘success’. We never mentioned that also. So there also it will take ‘success’ by default. Then both matches, result page rendered.<\/p>\n

<action name=\"\">\r\n    <result>home.jsp<\/result>\r\n<\/action><\/pre>\n

Another one important think we have to know what are all the background functions are running, while we call particular action. By default before running any action methods Struts 2 have a set of methods to run. Those are all called interceptors. We will discuss about interceptors in another chapter. As for now kepp in mind before running any action Struts 2 framework executing some methods.<\/p>\n

With the above point in mind we will see about how the data from home page is getting travelled to the result page. As I said before running the action method the struts 2 will look for the methods to pass the data from the html form to server.<\/p>\n

We will see this with an example. In our home.jsp we have to major data containing fields name and city.<\/p>\n

<s:textfield key=\"Name\" name=\"name\" \/>\r\n<s:textfield key=\"City\" name=\"city\" \/><\/pre>\n

So after we submit the data to the server the struts2 framework will look for the method setName() and setCity(). (i.e.) By default adding the word get infront of the field name and adding the field name starts with capital letter.<\/p>\n

Now to understand this see our Welcome.java methods.<\/p>\n

public void setName(String name) {\r\n    this.name = name;\r\n}\r\n\r\npublic void setCity(String city) {\r\n    this.city = city;\r\n}<\/pre>\n

We have to methods setName() and setCity(). So those will get executed before the action method executes. For each url access a new instance of the class will be created. So when multiple user opens the URL it will not affect one another.<\/p>\n

All the variables related to one session will be stored in a common stack called OGNL. So from any portion of the application we can get the value for the variables.<\/p>\n

In the same way in our welcome.jsp the tags\u00a0<s:property value=”name” \/><\/span>\u00a0 and\u00a0<s:property value=”city” \/><\/span>\u00a0 will call getName() and getCity() methods.<\/p>\n

Another one important think is all theses processes are happening in the server side. So only the replaced final html file will be sent back to the user. (i.e)The UI tags will not be visible at the user end.
\n\"action-post32\"
\n\"action-post31\"<\/p>\n

With this we will complete this session on Struts 2 action. We will continue with action-packages and namespaces in our next session.<\/p>\n

I have created a complete and simple .war file with source code for the example we discussed. Download and just place under Apache web apps folder and run.<\/p>\n

<\/p>\n