struts2拦截器

来源:互联网 发布:清迈打车软件 编辑:程序博客网 时间:2024/06/08 05:08

拦截器:

三种方法实现拦截器

①实现接口

interceptor

②扩展类

package com.mynews.intercept;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

public class TimerIntercept extends AbstractInterceptor {

    /**
     *
     */
    private static final long serialVersionUID = 1L;

    @Override
    public String intercept(ActionInvocation invocation) throws Exception {
        long start=System.currentTimeMillis();
        String result=invocation.invoke();//继续执行下一个拦截器,后续拦截器执行完之后才继续向下执行
        long end=System.currentTimeMillis();
        System.out.println(end-start);
        return result;

    }

}

<package name="news" namespace="/news" extends="struts-default">
    <!-- 声明一个拦截器 -->
        <interceptors>
            <interceptor name="timer" class="com.mynews.intercept.TimerIntercept"></interceptor>
            <interceptor-stack name="mystack"><!-- 将拦截器放入栈中 -->
                <interceptor-ref name="timer"></interceptor-ref> <!--自己定义的拦截器-->
                <interceptor-ref name="defaultStack"></interceptor-ref><!-- 引入系统默认的拦截器 -->
            </interceptor-stack>
        </interceptors>
    <!-- 将拦截器装备到action -->
        <default-interceptor-ref name="mystack"></default-interceptor-ref>

        <action name="*_NewsAction" class="com.mynews.action.NewsAction" method="{1}">
            <result name="main" type="dispatcher">/ch01/main.jsp</result>
            <!--  <result name="main" type="redirect">/ch01/main.jsp</result>  重定向方法    -->
        </action>
    </package>

③方法过滤拦截器

package com.mynews.intercept;

import java.util.Map;

import com.mynews.entity.Users;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;

public class RoleIntercept extends MethodFilterInterceptor {

    /**
     * 方法拦截器
     */
    private static final long serialVersionUID = 1L;

    @Override
    protected String doIntercept(ActionInvocation invocation) throws Exception {
        String result=null;
        ActionContext context=invocation.getInvocationContext();
        Map sessionMap=context.getSession();
        Users user=(Users) sessionMap.get("users");
        if(user==null){
            /*
             * ServletActionContext.getResponse().sendRedirect("指定页面");
             * return null;
             * */
            return Action.LOGIN;
        }else{
             result=invocation.invoke();
        }
        return result;

    }

}

<package name="news" namespace="/news" extends="struts-default">
    <!-- 声明一个拦截器 -->
        <interceptors>
            <interceptor name="timer" class="com.mynews.intercept.TimerIntercept"></interceptor>
            <!-- 方法拦截器 -->
            <interceptor name="role" class="com.mynews.intercept.RoleIntercept">
                <param name="excludeMethods">showNews,showNews2</param><!-- 不包含的方法 -->
                <param name="includeMethods">searchNewsList</param><!-- 包含的方法 -->
            </interceptor>

            <interceptor-stack name="mystack"><!-- 将拦截器放入栈中 -->
                <interceptor-ref name="timer"></interceptor-ref> <!--自己定义的拦截器-->
                <interceptor-ref name="role"></interceptor-ref>
                <interceptor-ref name="defaultStack"></interceptor-ref><!-- 引入系统默认的拦截器 -->
            </interceptor-stack>
        </interceptors>
    <!-- 将拦截器装备到action -->
        <default-interceptor-ref name="mystack"></default-interceptor-ref>
    <!-- 全局的结果 -->
        <global-results>
            <result name="login">/ch01/login.jsp</result>
        </global-results>

        <action name="*_NewsAction" class="com.mynews.action.NewsAction" method="{1}">
            <result name="main" type="dispatcher">/ch01/main.jsp</result>
            <!--  <result name="main" type="redirect">/ch01/main.jsp</result>  重定向方法    -->
        </action>
    </package>