struts2—过滤器interceptor

1.最简单的过滤器

  配置文件

<struts>
    <package name="interceptor" namespace="/interceptor" extends="struts-default">
        <interceptors>
            <!--1. 注册拦截器 -->
            <interceptor name="iterceptorDemo" class="cn.interceptor.demo.MyInterceptor"/>
            <!--2. 注册拦截器栈 -->
            <interceptor-stack name="myStack">
                <interceptor-ref name="iterceptorDemo"/>
                <interceptor-ref name="defaultStack" />
            </interceptor-stack>
        </interceptors>
        
        <!--3. 设置本包默认拦截器 -->
        <default-interceptor-ref name="myStack"/>
        <!-- 注册全局结果集 -->
        <global-results >
            <result name="toHello" type="redirect">/hello.jsp</result>
        </global-results>
        <action name="test1" class="cn.interceptor.demo.Action1" method="actionDemo1">
            <result name="success" type="dispatcher">/word.jsp</result>
        </action>
        
    </package>
</struts>

  过滤器

public class MyInterceptor extends MethodFilterInterceptor{
    private static final long serialVersionUID = 1L;
    @Override
    //通过继承MethodFilterInterceptor实现doIntercept直接加入到过滤器
    protected String doIntercept(ActionInvocation invocation) throws Exception {
        System.out.println("前拦截器");
        //走下一个过滤器
        invocation.invoke();
        System.out.println("后拦截器");
        return "toHello";
    }
}

  一个验证用户是否登录的实例

配置

<struts>
    <package name="customer" namespace="/" extends="struts-default" >
        <interceptors>
            <!-- 注册拦截器 -->
            <interceptor name="checkLogin" class="cn.crm.web.inercpetor.LoginInterceptor"/>
            <!-- 注册拦截器栈 -->
            <interceptor-stack name="myStack">
                <interceptor-ref name="checkLogin"/>
                <interceptor-ref name="defaultStack"/>
            </interceptor-stack>
        </interceptors>
        <!-- 设置本包的过滤器栈 -->
        <default-interceptor-ref name="myStack"/>
        <!-- 设置本包的全局的结果集 -->
        <!-- <global-results>
            <result name="tologin" type="redirect">/login.jsp</result>
        </global-results> -->
        <!-- 如果出现异常就跳转到error的结果集 -->
        <global-exception-mappings>
            <exception-mapping result="error" exception="java.lang.RuntimeException"/>
        </global-exception-mappings>
        <action name="customer_*" class="cn.crm.web.struts.CustomerList" method="{1}">
            <result name="list">/jsp/customer/list.jsp</result>
            <result name="tolist" type="redirectAction">
                <param name="actionName">customer_list</param>
                <param name="namespace">/</param>
            </result>
        </action>
        <action name="userAction_*" class="cn.crm.web.struts.UserAction" method="{1}">
            <result name="success" type="redirect">/index.htm</result>
            <result name="error" >/login.jsp</result>
        </action>
    </package>
</struts>

过滤器

public class LoginInterceptor extends MethodFilterInterceptor{
    private static final long serialVersionUID = 1L;
    @Override
    protected String doIntercept(ActionInvocation invocation) throws Exception {
        if(ActionContext.getContext().getSession().get("user")!=null) {
            invocation.invoke();
        }else {
            return "tologin";
        }
        return null;
}

猜你喜欢

转载自www.cnblogs.com/FlyBlueSky/p/9164997.html