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

Warning: Cannot modify header information - headers already sent by (output started at /home2/learnera/public_html/tech/wp-content/plugins/wptouch/core/class-wptouch-pro.php:82) in /home2/learnera/public_html/tech/wp-includes/rest-api/class-wp-rest-server.php on line 1831

Warning: Cannot modify header information - headers already sent by (output started at /home2/learnera/public_html/tech/wp-content/plugins/wptouch/core/class-wptouch-pro.php:82) in /home2/learnera/public_html/tech/wp-includes/rest-api/class-wp-rest-server.php on line 1831

Warning: Cannot modify header information - headers already sent by (output started at /home2/learnera/public_html/tech/wp-content/plugins/wptouch/core/class-wptouch-pro.php:82) in /home2/learnera/public_html/tech/wp-includes/rest-api/class-wp-rest-server.php on line 1831

Warning: Cannot modify header information - headers already sent by (output started at /home2/learnera/public_html/tech/wp-content/plugins/wptouch/core/class-wptouch-pro.php:82) in /home2/learnera/public_html/tech/wp-includes/rest-api/class-wp-rest-server.php on line 1831

Warning: Cannot modify header information - headers already sent by (output started at /home2/learnera/public_html/tech/wp-content/plugins/wptouch/core/class-wptouch-pro.php:82) in /home2/learnera/public_html/tech/wp-includes/rest-api/class-wp-rest-server.php on line 1831

Warning: Cannot modify header information - headers already sent by (output started at /home2/learnera/public_html/tech/wp-content/plugins/wptouch/core/class-wptouch-pro.php:82) in /home2/learnera/public_html/tech/wp-includes/rest-api/class-wp-rest-server.php on line 1831

Warning: Cannot modify header information - headers already sent by (output started at /home2/learnera/public_html/tech/wp-content/plugins/wptouch/core/class-wptouch-pro.php:82) in /home2/learnera/public_html/tech/wp-includes/rest-api/class-wp-rest-server.php on line 1831

Warning: Cannot modify header information - headers already sent by (output started at /home2/learnera/public_html/tech/wp-content/plugins/wptouch/core/class-wptouch-pro.php:82) in /home2/learnera/public_html/tech/wp-includes/rest-api/class-wp-rest-server.php on line 1831
{"id":363,"date":"2014-05-25T02:17:40","date_gmt":"2014-05-25T02:17:40","guid":{"rendered":"http:\/\/tech.learnerandtutor.com\/?p=363"},"modified":"2014-05-27T11:03:36","modified_gmt":"2014-05-27T11:03:36","slug":"struts2-tutorial-part-3-actions-1","status":"publish","type":"post","link":"http:\/\/tech.learnerandtutor.com\/struts2-tutorial-part-3-actions-1\/","title":{"rendered":"Struts2 Tutorial Part 3 \u2013 Actions 1"},"content":{"rendered":"

In this tutorial session we will see the important key aspects of Struts2 Actions. 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><\/p>\n

In nutshell, Struts2 actions are string returning java methods. Those methods are organized in a effective way for Struts2 Framework. See a sample action below.<\/p>\n

public String welcome() {\r\n    return \"success\";\r\n}<\/pre>\n

The above method is a valid struts2 action. So in short, any java method returns a string can be a struts2 action.<\/p>\n

Actions mainly do three thinks.<\/h4>\n
    \n
  1. Action Encapsulates the unit of work. (i.e) The real code logic for the application will be here.<\/li>\n
  2. Action returns control string to decide the result page. (i.e) Based on the result string from the action only the framework will decide which page to load (will explain later)<\/li>\n
  3. Action is the place where data transfer is happening. (will explain later)<\/li>\n<\/ol>\n

    So now we want to know how the framework executes the correct java method when request arrives. Here we take tomcat apache as our example server where our application is running. If suppose the user types the url\u00a0http:\/\/localhost:8080\/Struts2Action<\/span>\u00a0 in the browser it will come to the application server (i.e) here tomcat.\u00a0 Then the application server look for the application Struts2Action in its webapps folder.<\/p>\n

    Once it identifies it will look for web.xml file inside the application WEB_INF folder.Once it finds the web.xml it will read and execute based on that file.<\/p>\n

    As I explained in the HelloWorld Application<\/a> the web.xml will redirect all the requests it receives to struts filter (For Struts2 only; not all). Because we added a filter to all the requests. See a sample web.xml below.<\/p>\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    <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    <filter-mapping>\r\n        <filter-name>struts2<\/filter-name>\r\n        <url-pattern>\/*<\/url-pattern>\r\n    <\/filter-mapping>\r\n<\/web-app><\/pre>\n

    So here we redirected all the requests to org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter filter class. So the control will go to the org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter class. That class takes struts.xml as it’s configuration. So it will look for struts.xml in the class folder path.<\/p>\n

    See a sample struts.xml file 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=\"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        <\/action>\r\n    <\/package>\r\n<\/struts><\/pre>\n

    Once the struts.xml is found based on that configuration the flow continuous. Clear? As per our example once the user entered the URL\u00a0http:\/\/localhost:8080\/Struts2Action<\/span>\u00a0 the control will open this struts.xml file and tries to find corresponding action. In our example we never mentioned any name after the application Context Struts2Action.<\/em><\/p>\n

    But in struts.xml we mentioned a action with empty name as below.<\/p>\n

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

    So it will get match with our request http:\/\/localhost:8080\/Struts2Action<\/span>\u00a0. If suppose we type the url http:\/\/localhost:8080\/Struts2Action\/welcome<\/span> then the welcome action mentioned in the struts.xml will get executed. See that below.<\/p>\n

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

    Now we will see how to declare an action in the struts.xml. The syntax to declare an action is<\/p>\n

    <action name={actionName} [class={className}]>\r\n<result [name={resultValue1}]>{resultFileA}<\/result>\r\n<result [name={resultValue2}]>{resultFileB}<\/result>\r\n ...\r\n<result [name={resultValuen}]>{resultFileZ}<\/result>\r\n<\/action><\/pre>\n

    where<\/p>\n

    actionName is<\/h4>\n