In addition to using configuration file configurations, annotations can also be used to configure
Here are some common annotations
Introduction:
@Action/@Actions:
@Action specify a class as action, corresponding to the <action>....</action> tag in the profile, where you can configure the following properties
- Results: The result set property returned by the configuration is equivalent to the <result> list in struts2, which can be configured in {}, as follows
- Value: Configures the name of the action, equivalent to the name attribute in <action>
- 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/@Results:
@Result Configure the specific return results, used in results, or can be used on a separate class, with the following properties
- Name: Corresponds to the Name property in <result>
- Location: Correspondence between <result></result>
- 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