在Action中提供了下列几种注解:
ParentPackage
Namespace
Result
Results
注解
描述
Namespace
所期望的命名空间(在“struts.xml”文件中也有定义)的字符串值
ParentPackage
所期望的父package的字符串值
Results
“Result”注解列表
Result
提供了Action结果的映射,它有四个属性:
? name ——action方法的结果名
? type—— 结果类型
? value——任意的结果值。可以是rediect结果类型对应的action名,也可以是dispatcher结果类型对应的JSP
? parameters ——字符串参数组成的数组
@ParentPackage(value="struts-default") @Namespace(value="/test") @Result(name="success",value="/userSuc.jsp") @Results({ @Result(name="success",value="/userSuc.jsp",type=org.apache.struts2.dispatcher.ServletRedirectResult.class), @Result(name="input",value="/main.jsp",type=org.apache.struts2.dispatcher.ServletRedirectResult.class) })
<filter> <filter-name>struts</filter-name> <filter-class> org.apache.struts2.dispatcher.FilterDispatcher </filter-class> <init-param> <param-name>actionPackages</param-name> <param-value>user.actions,other.actions</param-value> </init-param> </filter>
public class TestAction extends ActionSupport { @Before public void doBefore(){ System.out.println("注解配置的方法在action执行之前调用"); } @After public void doAfter(){ System.out.println("注解配置的方法在action执行之后调用"); } @BeforeResult public void doBeforeResult(){ System.out.println("注解配置的方法在Result返回之前调用"); } ……. }
<package name="test" extends="struts-default"> <interceptors> <interceptor name="annoInteceptor" class="com.opensymphony.xwork2.interceptor.annotations.AnnotationWorkflowInterceptor"> </interceptor> ..... </interceptors> <action name="text" class="action.TestAction"> ..... <interceptor-ref name="annoInteceptor"></interceptor-ref> <interceptor-ref name="defaultStack"></interceptor-ref> </action> ....... </package>