Struts2 annotation summary ---- @ Action and @ Result
In addition to configuration file configuration, you can also use annotations to configure
Below are some common annotations
Introduction:
@ Action/@ Actions:
@ Action specifies a class as action, which corresponds to... in the configuration file ....Tag. You can configure the following attributes:
Results: configure the returned result set attribute, which is equivalent List, you can configure the attribute in {}, the specific value is as follows: configure the action name, equivalent to the name attribute interceptorRefs: configure the interceptor @ Action can be defined on the class, you can also define the method as follows (the role of @ Result can be discussed later, or you can work with it later)
@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:
/success.jsp
In xml configuration, if the name is not written, the default value is success. In the annotation, the default value is success if the name in results is not written.
You can also use @ Actions to specify multiple action mappings. In this way, you can map a class to multiple addresses, as shown below:
@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;}}You can use/testAction or/testAction2 to jump to success. jsp, because two action mappings are configured.
In xml configuration, we have the following configuration methods:
/{1}.jsp
This is the wildcard method in xml configuration. When we use add to access action, it will go to the add method of action for processing. When we return add, it will jump to add. the jsp page can be used without wildcards in the annotation, but it can also achieve similar effects. At this time, @ Action will be written on the method, as shown below:
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";}}In this way, the above effect is implemented, which means that @ Action can also be declared on the method (@ Actions can also be declared on the method)
@ Result/@ Results:
@ Result: configures the specific returned results, which can be used in results or separately in the class. The returned results have the following attributes:
Name: Corresponding Name attribute location in: corresponds Address type: Corresponding The type attribute @ Result can be declared on the class or configured with Action. If declared on the class, it is the global Result, as shown below:
@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 the page to jump to when the delete operation is returned, if @ Result is declared on the class, the @ Result above the class is found and then the delete. jsp page is redirected.
@ Results is used to declare multiple result sets. Its usage is similar to @ Actions.