码迷,mamicode.com
首页 > 其他好文 > 详细

Struts学习-拦截器2续

时间:2017-10-10 16:58:49      阅读:178      评论:0      收藏:0      [点我收藏+]

标签:blog   ext.get   exce   校验   com   web   etc   private   lob   

定义拦截器有2种办法:
1.实现Interceptor接口
2.集成AbstractInterceptor抽象类

一、方法1

.....

<struts>
    <package name="mypack" extends="struts-default">
<!--i.    j.    k.-->
        <interceptors>
            <interceptor name="loginInterceptor" class="com.nf.action.LoginInterceptor"></interceptor>
            <interceptor-stack name="myStack">
                <interceptor-ref name="loginInterceptor"></interceptor-ref>
                <interceptor-ref name="defaultStack"></interceptor-ref>
            </interceptor-stack>

        </interceptors>

        <default-interceptor-ref name="myStack"/>

        <global-results>
            <result name="login" type="redirectAction">userAction_loginView</result>
        </global-results>

        <!--不写method,111默认就是execute-->
        <action name="indexAction" class="com.nf.action.IndexAction">
            <result name="success">/WEB-INF/jsp/index.jsp</result>

            <interceptor-ref name="myStack"></interceptor-ref>
            <!--
            <interceptor-ref name="loginInterceptor"></interceptor-ref>
            <interceptor-ref name="defaultStack"></interceptor-ref>
            -->
        </action>

        <action ......
        </action>
    </package>

</struts>
package com.nf.action;

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

import java.util.Map;

/*
定义拦截器有2种办法:
1.实现Interceptor接口
2.集成AbstractInterceptor抽象类
 */
public class LoginInterceptor implements Interceptor {
    private Map<String,Object> session = null;
    public void destroy() {
    }
    public void init() {
    }
    public String intercept(ActionInvocation actionInvocation) throws Exception {
        Object myAction = actionInvocation.getAction();
        if(myAction instanceof UserAction){
            System.out.println("你访问的Action是UserAction,不要校验Session,否则死循环");
            //放行
            return actionInvocation.invoke();
        }else{
            System.out.println("你访问的Action是:"+myAction);
        }

        session = ActionContext.getContext().getSession();
        Object user = session.get("user");
        if (user!=null){
            return actionInvocation.invoke();
        }else{
            return "login";
        }
    }
}

二、方法2

package com.nf.action;

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

import java.util.Map;

/*
定义拦截器有2种办法:
1.实现Interceptor接口
2.集成AbstractInterceptor抽象类
 */
public class LoginInterceptor extends MethodFilterInterceptor {
    private Map<String, Object> session = null;

    protected String doIntercept(ActionInvocation actionInvocation) throws Exception {
        session = ActionContext.getContext().getSession();
        Object user = session.get("user");
        if (user!=null){
            return actionInvocation.invoke();
        }else{
            return "login";
        }
    }
}
......
<struts>
    <package name="mypack" extends="struts-default">
<!--i.    j.    k.-->
        <interceptors>
            <interceptor name="loginInterceptor" class="com.nf.action.LoginInterceptor"></interceptor>
            <interceptor-stack name="myStack">
                <interceptor-ref name="loginInterceptor">
                    <!--excludeMethods需要生效的话,
                    自定义的拦截器,不能使用实现Interceptor接口,
                    而是extends MethodFilterInterceptor
                    -->
                    <param name="excludeMethods">loginView,login</param>
                </interceptor-ref>
                <interceptor-ref name="defaultStack"></interceptor-ref>
            </interceptor-stack>

        </interceptors>

        <default-interceptor-ref name="myStack"/>

        <global-results>
            <result name="login" type="redirectAction">userAction_loginView</result>
        </global-results>

        <!--不写method,111默认就是execute-->
        <action name="indexAction" class="com.nf.action.IndexAction">
            <result name="success">/WEB-INF/jsp/index.jsp</result>

            <!-- <interceptor-ref name="myStack"></interceptor-ref> -->
            <!--
            <interceptor-ref name="loginInterceptor"></interceptor-ref>
            <interceptor-ref name="defaultStack"></interceptor-ref>
            -->
        </action>

        <action name="therFunctionAction" class="com.nf.action.OtherFunctionAction">
            <!--不写name,222默认就是success-->
            <result>/WEB-INF/jsp/otherFunction.jsp</result>
        </action>

        <action.....
        </action>
    </package>

</struts>

地址;https://gitee.com/MuNianShi/user5.git

Struts学习-拦截器2续

标签:blog   ext.get   exce   校验   com   web   etc   private   lob   

原文地址:http://www.cnblogs.com/junhua4254/p/7645620.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!