Also known as zero configuration (0 config), it eliminates the hassle of writing XML files and can be configured directly on the class call without having to switch back and forth between Java files and XML files.
You must import the Struts2-convention-plugin-2.3.15.jar package, which is in the Lib directory under the STRUTS2 installation package.
1. Action-level annotations
@ParentPackage--Parent package
This annotation is equivalent to the extends attribute in <package name= "login" extends= "Struts-default" ></package>, for example: @ParentPackage (value= " Struts-default ")
@Namespace----namespaces
This annotation is equivalent to
<package name= "Login" extends= "struts-default" namespace= "Logintest" ></package> namespace properties, Example: @ @Namespace (value= "/longspace")
@Result---Steering
This annotation is equivalent to
<action name= "Login" class= "Com.lwsoft.action.LoginAction" >
<result name= "Success" >/loginsuc.jsp</result>
</action>
In the <result name= "Success" >/loginsuc.jsp</result>
For example: @Result (name= "Success", location= "/loginsuc.jsp", type= "REDIRECT")!!! Note that in struts2.1.6 is the location to turn to the page, while the struts2.0.4 is the Value property
@Results
@Results ({
@Result (name= "Success", location= "/usersuc.jsp"),
@Result (name= "error", location= "/usererror.jsp")
}) to configure multiple steering
The above is a simple introduction to struts2 annotations,
To use annotations on an action, you need to repair some of the configuration in the Web. xml file
<Filter-name>Struts</Filter-name> <Filter-class>Org.apache.struts2.dispatcher.FilterDispatcher</Filter-class> <!--//fixed format - </Init-param> <Param-name>Actionpackages</Param-name> <!--Com.struts2.action1,com.struts.action2 - <Param-value>Com.struts2.action</Param-value> </Init-param></Filter> <!--The package that the action is in, if it is in more than one package, separated by commas <init-param>
< Span id= "1_10" > @Results ({
@Result (name= "input" value= "/input.jsp" type= Nullresult.class),
@Reuslt (name= "Success" value= "/success.jsp" Type=nullresult.class),
@Result (name= "error "Value="/error.jsp "Type=nullresult.class)
})
above is a typical multiple result configuration, the Name property specifies the string returned, value specifies the page to jump from, The type specifies the kind of result, type is a very important property, he has a few situations:
1) Nullresult: Default can be omitted
2) Actionchainresult: Used to jump from one action to another action
for example:
@Result
(name = "Search", value= "search", Type=actionchainresult.class,
params={"Method", "Search"})
This configuration is if the return value of "search" jumps off SearchAction's search method, if you do not specify the params jump to SearchAction's Execute Method
3) Streamresult: For file download
For example:
@Result
(name= "Success", value= "InputStream", Type=streamresult.class,
params={" BufferSize ", fileconstant.download_buffer_size})
Struts2 Annotation Annotation configuration