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

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: 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: 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: 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
Struts2 Tutorial Part 3 – Actions 2 | Rajesh

Struts2 Tutorial Part 3 – Actions 2


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

Deprecated: Creation of dynamic property WP_Query::$yarpp_cache_type is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/yet-another-related-posts-plugin/classes/YARPP_Cache.php on line 30

Deprecated: Creation of dynamic property WP_Post::$score is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-post.php on line 266

Deprecated: Creation of dynamic property WP_Post::$score is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-post.php on line 266

Deprecated: Creation of dynamic property WP_Post::$score is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-post.php on line 266

Deprecated: Creation of dynamic property WP_Post::$score is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-post.php on line 266

Deprecated: Creation of dynamic property WP_Post::$score is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-post.php on line 266

Deprecated: Creation of dynamic property WP_Post::$score is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-post.php on line 266

Deprecated: Creation of dynamic property WP_Post::$score is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-post.php on line 266

Deprecated: Creation of dynamic property WP_Post::$score is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-post.php on line 266

Deprecated: Creation of dynamic property WP_Post::$score is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-post.php on line 266

Deprecated: Creation of dynamic property WP_Post::$score is deprecated in /home2/learnera/public_html/tech/wp-includes/class-wp-post.php on line 266

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

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. If you want to get clear idea over struts 2 from the beginning please refer the below posts.

Struts2 Introduction
Struts2 HelloWorld Application
Struts2 Tutorial Part 3 – Actions 1

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.

home.jsp

<%@ taglib prefix="s" uri="/struts-tags" %>
    <html>

    <head>
        <title>Struts 2 Custom URL</title>
        <script src="jquery-1.11.1.min.js"></script>
    </head>

    <body>
        <h1>Struts 2 Action Tutorial</h1>
        <s:form action="welcome" method="post" enctype="multipart/form-data">
            <s:textfield key="Name" name="name" />
            <s:textfield key="City" name="city" />
            <s:submit key="submit" />
        </s:form>
    </body>

    </html>

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 <s:form>  as normal <form>  tag, <s:textfield>  as normal <input type=’text’>  tag and so.So when we fill and submit the form it will submit the data to the url welcome.

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.

welcome.jsp

<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
    <title>Struts2 Custom URL</title>
</head>

<body>
    <h2>
Welcome to Struts2 Actions Tutorial
<s:property value="name" />,<s:property value="city" />.
</h2>
    <a href="/Struts2Action">Go Back to Home</a>
</body>

</html>

Here also we used struts2 UI  tag <s:property> . 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.

Next we will create our struts.xml file which is the configuration file, See the code below.

struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
    <package name="default" namespace="/" extends="struts-default">
        <action name="">
            <result>home.jsp</result>
        </action>
        <action name="welcome" class="com.rajesh.struts2.Welcome" method="welcome">
            <result name="success">welcome.jsp</result>
            <result name="error">error.jsp</result>
        </action>
    </package>
</struts>

See the web.xml file below.

web.xml

<?xml version="1.0" encoding="UTF-8" ?>
<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">
    <display-name>Struts2Action</display-name>

    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

</web-app>

Finally see our java file where we have our business logic.

Welcome.java

package com.rajesh.struts2;

import com.opensymphony.xwork2.ActionSupport;

public class Welcome extends ActionSupport {

    private static final long serialVersionUID = 1500617172067606799L;

    private String name, city;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String welcome() {
        return SUCCESS;
    }
}

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 http://localhost:8080/Struts2Action/  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.

<filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>

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.  Just for understanding I am explaining this.

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.

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.

After rendering the page the page source code will look like below one.

<html>

<head>
    <title>Struts 2 Custom URL</title>
</head>

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

        </table>
    </form>
</body>

</html>

Then  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 . As usual the control will look for an action with name ‘welcome’. Once it identifies it will execute the corresponding class to it.

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.

Note:

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). If no class is specified it will take the result as ‘success’ by default

Another attribute for the action tag is method name. See our action welcome.

<action name="welcome" class="com.rajesh.struts2.Welcome" method="welcome">

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.

What is the use of this?

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.

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.

<action name="">
    <result>home.jsp</result>
</action>

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.

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.

We will see this with an example. In our home.jsp we have to major data containing fields name and city.

<s:textfield key="Name" name="name" />
<s:textfield key="City" name="city" />

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.

Now to understand this see our Welcome.java methods.

public void setName(String name) {
    this.name = name;
}

public void setCity(String city) {
    this.city = city;
}

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.

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.

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

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.
action-post32
action-post31

With this we will complete this session on Struts 2 action. We will continue with action-packages and namespaces in our next session.

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.

If you have any issues while execution post in comments or send mail to rajeshmepco@gmail.com.


Deprecated: Creation of dynamic property WP_Term::$cat_ID is deprecated in /home2/learnera/public_html/tech/wp-includes/category.php on line 378

Deprecated: Creation of dynamic property WP_Term::$category_count is deprecated in /home2/learnera/public_html/tech/wp-includes/category.php on line 379

Deprecated: Creation of dynamic property WP_Term::$category_description is deprecated in /home2/learnera/public_html/tech/wp-includes/category.php on line 380

Deprecated: Creation of dynamic property WP_Term::$cat_name is deprecated in /home2/learnera/public_html/tech/wp-includes/category.php on line 381

Deprecated: Creation of dynamic property WP_Term::$category_nicename is deprecated in /home2/learnera/public_html/tech/wp-includes/category.php on line 382

Deprecated: Creation of dynamic property WP_Term::$category_parent is deprecated in /home2/learnera/public_html/tech/wp-includes/category.php on line 383
This entry was posted in Struts2 and tagged , , . Bookmark the permalink.

Deprecated: Creation of dynamic property WP_Query::$comments_by_type is deprecated in /home2/learnera/public_html/tech/wp-includes/comment-template.php on line 1527

Leave a Reply

Your email address will not be published.


*


This site uses Akismet to reduce spam. Learn how your comment data is processed.