Struts2 拦截器配置和实例

拦截器只能拦截Action
(1)自定义器类要实现接口Interceptor,实现相应的方法。

拦截器的init()是在服务器一启动的时候就调用的。拦截器初始化时调用。只调用一次。

destory()拦截器被销毁之前调用。

整个拦截器的核心部分是invocation.invoke()这个函数和这个函数的调用位置 。

invocation.invoke()函数调用之前代码将会在Action之前被依次执行。而在invocation.invoke()之后的代码,将会在Action之后被执行。 如果不需要调用后续的方法,则返回一个String类型的对象即可。

public class TimerInterceptor implements Interceptor {

    /**
     * Called to let an interceptor clean up any resources it has allocated.
     */
    public void destroy() {
    }

    /**
     * Called after an interceptor is created, but before any requests are
     * processed using
     * {@link #intercept(com.opensymphony.xwork2.ActionInvocation) intercept} ,
     * giving the Interceptor a chance to initialize any needed resources.
     */
    public void init() {
    }

    /**
     * Allows the Interceptor to do some processing on the request before and/or
     * after the rest of the processing of the request by the
     * {@link ActionInvocation} or to short-circuit the processing and just
     * return a String return code.
     * 
     * @return the return code, either returned from
     *         {@link ActionInvocation#invoke()}, or from the interceptor
     *         itself.
     * @throws Exception
     *             any system-level error, as defined in
     *             {@link com.opensymphony.xwork2.Action#execute()}.
     */

    public String intercept(ActionInvocation invocation) throws Exception {
        // TODO Auto-generated method stub
        long startTime = System.currentTimeMillis();
        String result = invocation.invoke();
        long executionTime = System.currentTimeMillis() - startTime;
        System.out.println("Action 的执行花费时间:" + executionTime);
        return result;
    }
}

struts.xml配置

<struts>
    <package name="strutsqs" extends="struts-default">

        <interceptors>
            
            <interceptor name="time" class="com.interceptor.TimerInterceptor"></interceptor>

        </interceptors>
        

        <action name="Login" class="com.action.LoginAction" method="execute">
            <result name="error">/page/login/error.jsp</result>
            <result name="success" type="chain">GetBooks</result>
            <interceptor-ref name="time"></interceptor-ref>
        </action>

        <action name="GetBooks" class="com.action.GetBooksAction">
            <result name="login">/page/login/login.jsp</result>
            <result name="success">/page/show/showBook.jsp</result>
        </action>

    
    </package>
</struts>

action 的拦截器只有一个,所以invocation.invoke()会调用action的方法。
结果如图:

(2)拦截器栈的使用

栈的使用,就是先进后出。

如果调用invocation.invoke()方法,后面还有拦截器,则会继续调用下一个拦截器。

代码中,会按照栈中定义的拦截器一次执行time、permisision。

struts2.xml

<interceptors>
            <interceptor name="time" class="com.interceptor.TimerInterceptor"></interceptor>
            <interceptor name="permission" class="com.interceptor.PermissionInterceptor"></interceptor>
            <interceptor-stack name="bothinterceptor">
                <interceptor-ref name="time"></interceptor-ref>
                <interceptor-ref name="permission"></interceptor-ref>
                <interceptor-ref name="defaultStack"></interceptor-ref>
            </interceptor-stack>
        </interceptors>

        <!--
            <default-interceptor-ref name="bothinterceptor"/>这句是设置在同一个package下所有Action自动调用的拦截器堆栈-->
        <action name="Login" class="com.action.LoginAction" method="execute">
            <result name="error">/page/login/error.jsp</result>
            <result name="success" type="chain">GetBooks</result>
            <interceptor-ref name="bothinterceptor"></interceptor-ref>
        <<!--
需要使用那个拦截器,就可以写那个拦截器,
          也可以写成<interceptor-ref name="permission"></interceptor-ref>,说明对于action只使用permission拦截器
也可以同时写多个,按照相应的顺序执行
-->>
</action> <action name="GetBooks" class="com.action.GetBooksAction"> <result name="login">/page/login/login.jsp</result> <result name="success">/page/show/showBook.jsp</result> </action>

结果如图:

(3)默认拦截器的使用

简单的使用:

<action name="Login" class="com.action.LoginAction" method="execute">
   <result name="error">/page/login/error.jsp</result>
   <result name="success" type="chain">GetBooks</result>

<!--
             <interceptor-ref name="默认拦截器名或拦截器栈名"/>
  -->
   <interceptor-ref name="logger"/>
  </action> 

同样可以把 <interceptor-ref name="默认拦截器名"/>加入到拦截器栈标签中。

posted @ 2013-12-04 15:09  jiaokaige  阅读(667)  评论(1编辑  收藏  举报