Struts2拦截器

这里我们建立了一个拦截器

package com.cj.interceptor;

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

//拦截器
public class FirstInterceptor implements Interceptor{
    private String someThing;
    public String getSomeThing() {
        return someThing;
    }

    public void setSomeThing(String someThing) {
        this.someThing = someThing;
    }

    @Override
    public void destroy() {
        // TODO Auto-generated method stub

    }

    /**
     * 初始化方法 当程序启动的时候拦截器就被初始化完毕了
     */
    @Override
    public void init() {
        System.out.println(someThing+"拦截器启动了");

    }

    @Override
    public String intercept(ActionInvocation arg0) throws Exception {
        // TODO Auto-generated method stub
        //arg0.invoke()让拦截器的请求继续前进,访问需要访问的资源
        //放过去
        System.out.println("进入First拦截器");
        String returnName = arg0.invoke();
        System.out.println("走出First拦截器");
        return returnName;
    }

}

struts.xml里面配置拦截器

<interceptors>
            <interceptor name="FirstInter" class="com.cj.interceptor.FirstInterceptor">
                <!-- 如果需要初始化值才需要这个 -->
                <param name="someThing">admin</param>
            </interceptor>

        </interceptors>

在struts.xml里面的action中

<action name="loginAction" class="com.cj.action.LoginAction">
            <interceptor-ref name="FirstInter"></interceptor-ref>
            <!-- 默认的拦截器一定要加 -->
                <interceptor-ref name="defaultStack"></interceptor-ref>
            <result name="success">/Login.jsp</result>
        </action>

当有多个拦截器

<interceptors>
            <interceptor name="FirstInter" class="com.cj.interceptor.FirstInterceptor">
                <!-- 如果需要初始化值才需要这个 -->
                <param name="someThing">admin</param>
            </interceptor>

            <interceptor name="SecondInter" class="com.cj.interceptor.SecondInterceptor">

            </interceptor>

        </interceptors>

在struts.xml里面的action中

<action name="loginAction" class="com.cj.action.LoginAction">
            <interceptor-ref name="FirstInter"></interceptor-ref>
            <interceptor-ref name="SecondInter"></interceptor-ref>
            <!-- 默认的拦截器一定要加 -->
                <interceptor-ref name="defaultStack"></interceptor-ref>
            <result name="success">/Login.jsp</result>
        </action>

如果有多个拦截器 action里面会有多个类似

<interceptor-ref name="SecondInter"></interceptor-ref>

所以有了拦截器的堆栈的概念

<interceptors>
            <interceptor name="FirstInter" class="com.cj.interceptor.FirstInterceptor">
                <!-- 如果需要初始化值才需要这个 -->
                <param name="someThing">admin</param>
            </interceptor>

            <interceptor name="SecondInter" class="com.cj.interceptor.SecondInterceptor">

            </interceptor>

            <interceptor-stack name="AllInterceptor">
                <interceptor-ref name="FirstInter"></interceptor-ref>
                <interceptor-ref name="SecondInter"></interceptor-ref>

                <interceptor-ref name="defaultStack"></interceptor-ref>
            </interceptor-stack>

        </interceptors>

在struts.xml里面的action中只要

<action name="loginAction" class="com.cj.action.LoginAction">
            <interceptor-ref name="AllInterceptor"></interceptor-ref>
            <result name="success">/Login.jsp</result>
        </action>

如果要所有的action都有默认的拦截器

<!-- 所有action默认的拦截器 及系统和默认的拦截器 -->
        <default-interceptor-ref name="AllInterceptor"></default-interceptor-ref>

都有默认的action返回的结果

<global-results>
            <result name="error">/error.jsp</result>
        </global-results>

方法拦截器

package com.cj.interceptor;

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

//方法拦截器
public class MethodInterceptor extends MethodFilterInterceptor{

    @Override
    protected String doIntercept(ActionInvocation arg0) throws Exception {
        System.out.println("进入method方法拦截器");
        String url = arg0.invoke();
        System.out.println("走出method方法拦截器");
        return url;
    }

}

struts.xml里面配置

<interceptor name="MethodInter" class="com.cj.interceptor.MethodInterceptor">
               <!-- 拦截add,delete方法 -->
                <param name="includeMethods">add,delete</param>

                <!-- 除了show没有拦截器 其余的都去拦截 -->
                <param name="excludeMethods">show</param>
            </interceptor>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

卡搜偶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值