Summary of struts2 annotations----@Action and @result

Source: Internet
Author: User

In addition to using configuration file configurations, annotations can also be used to configure

Here are some common annotations



@Action specify a class as action, corresponding to the <action>....</action> tag in the profile, where you can configure the following properties

    1. Results: The result set property returned by the configuration is equivalent to the <result> list in struts2, which can be configured in {}, as follows
    2. Value: Configures the name of the action, equivalent to the name attribute in <action>
    3. Interceptorrefs: Configuring Interceptors
@Action can be defined on a class, or it can be defined on a methodas follows (the function of the @Result, can also be seen with the back)
@Action (value = "Testaction", results = {@Result (name= "Success", location= "/success.jsp")}) public class Testaction Extends Actionsupport {@Overridepublic String execute () throws Exception {return SUCCESS;}}
This is equivalent to the following XML configuration
<action name= "testaction" class= "struts2.action.testAction" ><result name= "Success" >/success.jsp</ Result></action>
In the XML configuration if the name is not written, then the default is success, in the annotations also, if the name in results is not written, then the default is success
You can also use @actions to specify multiple action mappings so that a class corresponds to multiple address mappings, as follows
@Actions ({@Action (value = "Testaction", results = {@Result (location= "/success.jsp")}), @Action (value = "TestAction2", Results = {@Result (location= "/success.jsp")}) public class Testaction extends Actionsupport {@Overridepublic String Execute () throws Exception {return SUCCESS;}}
This is used/testaction or/testaction2 to jump to success.jsp because two action mappings are configured

in the XML configuration, we have the following configuration methods
<action name= "*" class= "struts2.action.testAction" Method={1}><result name= "{1}" >/{1}.jsp</result ></action>
This is a wildcard in the XML configuration, that is, when we access the action with ADD, we will go to the action's Add method to process it and jump to the add.jsp page when we return to add.There are no wildcards to use in annotations, but similar effects can be achieved, when @action is written on the method, as in the following
public class Testaction extends Actionsupport {@Action (value = "add", results = {@Result (name= "Add", location= "/add.jsp") }) Public String Add () throws Exception {return "add";} @Action (value = "Delete", results = {@Result (name= "Delete", location= "/delete.jsp")}) public String Delete () throws Exception {return "delete";}}
This achieves the above effect, which means that @action can also be declared on a method (@Actions can also be declared on a method)


@Result Configure the specific return results, used in results, or can be used on a separate class, with the following properties

    1. Name: Corresponds to the Name property in <result>
    2. Location: Correspondence between <result></result>
    3. Type: The Type property of the corresponding <result>
@Result can be declared on a class, and the action configuration Declaration, if declared on the class, is the result of the global, as follows
@Result (name= "Delete", location = "/delete.jsp") public class Testaction extends Actionsupport {@Action (value = "Add", Results = {@Result (name = "Add", location = "/add.jsp")}) public String Add () throws Exception {return "add";} @Action (value = "Delete") public String Delete () throws Exception {return "delete";}}
Although the Delete method does not specify which page page to jump to when the delete is returned, but is declared with @result on the class, the @result is found on the class and then jumps to the delete.jsp page

@Results is used to declare multiple result sets, and the usage is similar to @actions, which is no longer detailed

Struts2 Comments Summary [email protected] and @result

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