Struts2拦截器

来源:互联网 发布:淘宝怎么不能买wlan了 编辑:程序博客网 时间:2024/06/03 19:06

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

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>
0 0
原创粉丝点击