Java学习笔记程序员

10、自定义拦截器

2016-12-04  本文已影响337人  MPPC

1、Struts2 拦截器

2、 Struts2自带的拦截器

3、Interceptor 接口

public interface Interceptor extends Serializable {
 
    void destroy();
    void init();
    String intercept(ActionInvocation invocation) throws Exception;
 
}

4、自定义拦截器

public class MyInterceptor extends AbstractInterceptor{

    public String intercept(ActionInvocation invocation) throws Exception {
        System.out.println("before invocation.invoke...");

        String result = invocation.invoke();
        System.out.println("after invocation.invoke...");
        return result;
        //return "success"; 直接返回一个值,将不会调用后面的拦截器
    }

}
 <interceptors>
   <!-- 配置拦截器 -->
   <interceptor name="myInterceptor" class="org.pan.struts.interceptor.MyInterceptor"/>
   <interceptor-stack name="myIntercepterStack">
       <interceptor-ref name="paramsPrepareParamsStack"/>
   </interceptor-stack>
</interceptors>
<action name="user_*" class="org.pan.struts.action.TestValidationAction" method="{1}">
    <!--使用拦截器 -->
    <interceptor-ref name="myInterceptor"/>
    <!-- 还需要引用拦截器器栈 -->
    <interceptor-ref name="myIntercepterStack"/>

    <result name="success">/WEB-INF/views/success.jsp</result>
    <result name="{1}">/WEB-INF/views/{1}.jsp</result>
    <result name="input">/WEB-INF/views/input.jsp</result>
</action>
上一篇 下一篇

猜你喜欢

热点阅读