struts2拦截器注解<struts> <package name="default" extends="struts-default"> <interceptors> <interc

来源:互联网 发布:梦幻西游mac版好不好用 编辑:程序博客网 时间:2024/05/21 23:00

使用struts2的@Before,@After,@BeforeResult注解标注方法,可以使该方法在action的处理方法执行前或执行后的某一点上被执行

Before:标注的方法在action的方法执行之前被调用,如果有返回值,并且不为null,则这个返回值将作为action的结果代码

After:标注的方法在action的方法执行后执行,,如果有返回值,这个返回值将被忽略。

BeforeResult标注的方法在action之后在result之前执行,如果标注的方法有返回值将被忽略。


struts.xml文件

<struts>
<package name="default" extends="struts-default">
<interceptors>
<interceptor name="annotationInterceptor"
class="com.opensymphony.xwork2.interceptor.annotations.AnnotationWorkflowInterceptor">
</interceptor>

<interceptor-stack name="annotationStack">
<interceptor-ref name="defaultStack"></interceptor-ref>
<interceptor-ref name="annotationInterceptor"></interceptor-ref>
</interceptor-stack>
</interceptors>
<action name="annotationAction" class="annotations.interceptor.AnnotationAction">
<result name="success">/index.jsp</result>
<interceptor-ref name="annotationStack"></interceptor-ref>
</action>
    
</package>
</struts>

action代码:

@Before
public void before(){
System.out.println("执行before方法");
}
@After
public void after(){
System.out.println("执行after方法");
}
@BeforeResult
public void beforeResult(){
System.out.println("执行beforeResult方法");
}
@Override
public String execute(){
System.out.println("执行execute方法");
return SUCCESS;
}

打印结果:

执行before方法
执行execute方法
执行beforeResult方法
执行after方法

0 0