struts2拦截器

拦截器:

三种方法实现拦截器

①实现接口

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>



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值