The biggest advantage of using annotations to configure actions is that zero configuration can be implemented, but transactions have both advantages and disadvantages. This makes it easier to use and maintain.
To use annotation, we must add an additional package: struts2-convention-plugin-2.x.x.jar.
Although the configuration is zero, struts. xml is indispensable. The configuration is as follows:
<? Xml version = "1.0" encoding = "UTF-8"?>
<! DOCTYPE struts PUBLIC
"-// Apache Software Foundation // DTD Struts Configuration 2.1.7 // EN"
Http://struts.apache.org/dtds/struts-2.1.7.dtd>
<Struts>
<! -- Encoding method of Request Parameters -->
<Constant name = "struts. i18n. encoding" value = "UTF-8"/>
<! -- Specify the request suffix type processed by struts2. Separated by commas -->
<Constant name = "struts. action. extension" value = "action, do, htm"/>
<! -- Whether to reload the struts. xml file after it is modified. The default value is false (used in the production environment). It is best to enable it in the development stage -->
<Constant name = "struts. configuration. xml. reload" value = "true"/>
<! -- Whether to use the struts development mode. The development mode has more debugging information. The default value is false (used in the production environment). It is best to enable it in the development stage -->
<Constant name = "struts. devMode" value = "false"/>
<! -- Sets whether the browser caches static content. The default value is true (used in the production environment). It is best to disable it in the development stage -->
<Constant name = "struts. serve. static. browserCache" value = "false"/>
<! -- Specify that spring is responsible for creating action objects.
<Constant name = "struts. objectFactory" value = "spring"/>
-->
<! -- Whether to enable dynamic method call -->
<Constant name = "struts. enable. DynamicMethodInvocation" value = "false"/>
</Struts>
Annotation of action class:
Package com. tjcyjd. web. action;
Import org. apache. struts2.convention. annotation. Action;
Import org. apache. struts2.convention. annotation. ExceptionMapping;
Import org. apache. struts2.convention. annotation. ExceptionMappings;
Import org. apache. struts2.convention. annotation. Namespace;
Import org. apache. struts2.convention. annotation. ParentPackage;
Import org. apache. struts2.convention. annotation. Result;
Import org. apache. struts2.convention. annotation. Results;
Import com. opensymphony. xwork2.ActionSupport;
/**
* Struts2 annotation-based Action configuration
*
*/
@ ParentPackage ("struts-default ")
@ Namespace ("/annotation_test ")
@ Results ({@ Result (name = "success", location = "/main. jsp "),
@ Result (name = "error", location = "/error. jsp ")})
@ ExceptionMappings ({@ ExceptionMapping (exception = "java. lange. RuntimeException", result = "error ")})
Public class LoginAction extends ActionSupport {
Private static final long serialVersionUID = 2730268055700929183L;
Private String loginName;
Private String password;
@ Action ("login") // or @ Action (value = "login ")
Public String login () throws Exception {
If ("yjd". equals (loginName) & "yjd". equals (password )){
Return SUCCESS;
} Else {
Return ERROR;
}
}
@ Action (value = "add", results = {@ Result (name = "success", location = "/index. jsp ")})
Public String add () throws Exception {
Return SUCCESS;
}
Public String getLoginName (){
Return loginName;
}
Public void setLoginName (String loginName ){
This. loginName = loginName;
}
Public String getPassword (){
Return password;
}
Public void setPassword (String password ){
This. password = password;
}
}
This completes an annotation-based action configuration.
Common annotations are summarized as follows:
Namespace: Specify the Namespace.
ParentPackage: Specifies the parent package.
Result: provides the Action ing of Action results. (A result ing)
Results: "Result" annotation list
ResultPath: Specify the base path of the result page.
Action: Specifies the access URL of the Action.
Actions: "Action" annotation list.
ExceptionMapping: Specifies the exception ing. (An error occurred while ing a declaration)
Predictionmappings: An array with an exception declared at the first level.
InterceptorRef: the interceptor reference.
InterceptorRefs: the interceptor reference group.