Struts2 Framework Learning II: Action detailed __struts2

Source: Internet
Author: User
Tags aliases

Preface

In Struts 2, the action is its core function, using the Struts 2 framework, where the main development is done around the action, The action we write usually needs to implement the Com.opensymphony.xwork2.Action interface, and the method that needs to be implemented is the Execute method, but in real development, the action can be written without having to implement the action interface, but instead create a normal Java class and add You can add the Execute method to public String execute () {return ' success '; Another way is to integrate the Actionsupport class, which is located under Com.opensymphony.xwork2 and implements the Execute method of the action interface. The following two ways are most commonly used.

Around the action, it is divided into the following sections:

Action Property
Dynamic method call
Default Action Configuration
Wildcard mapping

Action Property

The action has a mapping framework, mainly the URL mapping to the corresponding action class, the action configuration is mainly written in the Struts.xml file, all the action attributes are as follows:

Property is required Description
Name Is The name of the action, used to match the requested URL
Class Whether The full class name of the action implementation class
Method Whether Call the method specified in the Action implementation class
Converter Whether The full class name applied to the action type converter

It should be noted that the Name property of the action is generally not allowed. or/or-. But the underline is OK. In addition, if you do not configure the class full class name for the action in the configuration file, the framework invokes the Execute method in the Actionsupport class, which is implemented simply by returning a success, which is equivalent to forwarding, so this is actually associated with struts The role of the Actionforward in 1 is consistent. So we can use this feature to implement the action that only needs to be done with forwarding, which is much more convenient than using Actionforward.

The following focuses on the method attribute description

The action is usually done to complete a function point, but for example crud such operations using four action classes is obviously not cost-effective, in struts 2 you can map these four functions into an action to process, where you need to use the method attribute. You can implement CRUD mapping to the same action by specifying different aliases for an action using the method attribute and the Name property in the Struts.xml configuration file.

For example, there is a user management module, users need to add, modify, delete and query, we first create the Useraction class, as follows:

Package action;

Import Com.opensymphony.xwork2.ActionSupport;

public class Useraction extends actionsupport{

    private static final long serialversionuid = 1L;

    Query all users public
    String list () throws Exception {return
        SUCCESS;
    }

    Modify user Information public
    String update () {return
        SUCCESS;
    }

    Delete User Information public
    String Delete () {return
        SUCCESS;
    }

    Add user public
    String Add () {return
        SUCCESS;
    }
}

Note that you don't necessarily need the Execute method in the action, you can specify the method you want, and after the action is written, you need to write the configuration file in the Struts.xml file, as follows:

<package name= "user" namespace= "/user" extends= "Struts-default" >
        <action "List" name= "action. Useraction "method=" list ">
            <result>/list.jsp</result>
        </action>
        <action Name = "Add" class= "action. Useraction "method=" Add ">
            <result>/add.jsp</result>
        </action>
        <action name=" Delete the "class=" action. Useraction "method=" delete ">
            <result>/delete.jsp</result>
        </action>
        <action Name= "Update" class= "action. Useraction "method=" Update ">
            <result>/update.jsp</result>
        </action>
    </ Package>

Here, by using aliases to map multiple business functions to a different approach to an action, this is a bit clumsy but logical and straightforward, but in the framework China also provides different ways of writing configuration files that can be mapped to action. This is the dynamic method call that you want to talk about dynamically (invocation, or DMI, for short).

Dynamic method Call

The so-called dynamic method invocation is the use of an exclamation mark in the name of the action! Identifies the method name that needs to be invoked, and the calling format is actionname! Actionmethod.action, this way can be real method of dynamic invocation, the following to do a test of this conclusion, first of all, the configuration file to make a small change:

<package name= "user" namespace= "/user" extends= "Struts-default" "> <action name=" useraction "class="
        Action. Useraction ">
            <result name=" Success ">/list.jsp</result>
            <result name=" Update ">/ update.jsp</result>
            <result name= "Add" >/add.jsp</result>
        </action>
        <!-- <action name= "Add" class= "action. Useraction "method=" Add ">
            <result>/add.jsp</result>
        </action>
        <action name=" Delete the "class=" action. Useraction "method=" delete ">
            <result>/delete.jsp</result>
        </action>
        < Action name= "Update" class= action. Useraction "method=" Update ">
            <result>/update.jsp</result>
        </action>-->
    </package>

Then modify the useraction as follows:

Query all users public
    String list () throws Exception {return
        SUCCESS;
    }

    Modify user Information public
    String update () {return
        ' update ';
    }

    Delete User Information public
    String Delete () {return
        null;
    }

    Add user public
    String Add () {return
        ' Add ';
    }

Here is the test result:

It's important to note that although DMI brings a lot of traversal to development, there are security risks that can be easily exploited by a URL that allows you to place any method in the action directly. In this case, security control mechanisms need to be used.

The problem with how the method property and DMI should be used can be summarized as follows: If the different methods in an action use the same configuration (same result and interceptor configuration), then you can use the DMI Otherwise, use the method property to configure in the Struts.xml file.

the default action

The default action is the action that is accessed when a nonexistent action is accessed, and the default action is configured by simply adding the following configuration to the package:

<package name= "Default" namespace= "/" extends= "Struts-default" >
        <default-action-ref name= "Hello"/>
        <!--HelloWorld Demo program-->
        <action name= "Hello class=" example. HelloWorld ">
            <result>/index.jsp</result>
        </action>
    </package>

The action here is configured under package, and note that each package can have a default action, but each namespace should have only one default action, because the framework does not know to access the default action if multiple words are used.

Wildcard Mapping

The use of wildcard mapping can greatly reduce the number of action, the so-called wildcard is the use of *, to match 0 or more characters. In the configuration of the action, you can use * to match any character for the Name property. For example, the following configuration:

<action name= "edit*" class= "action. Edit{1}action ">
            <result>/{1}.jsp</result>
        </action>

{1} is populated with the contents of the * in the Name property, such as accessing/edituser in the browser and mapping to the action. Edituseraction class, return to the user.jsp page. where {1} is actually a placeholder, the value of the curly braces can be 0-9, where {0} represents the entire request URL. For example, the following configuration:

<action name= "*_*" class= "action. {1} Action "method=" {2} ">
            <result>/{1}_{2}.jsp</result>
        </action>

When accessing user_list, it maps to the Useraction class, accesses the list method in Useraction, and returns the resulting page user_list.jsp.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.