zoukankan      html  css  js  c++  java
  • j2ee之struts2拦截器()

    struts2的基本配置以及jar包的导入就不说了只写关键部分:

    <struts>
        <package name="userPackage" extends="struts-default">
    <interceptors>
          <!-- 自己写的一个拦截器 -->
    <interceptor name="loginCheck" class="com.xinzhi.interceptor.MyInterceptor"></interceptor> <interceptor-stack name="login_interceptor"> <interceptor-ref name="defaultStack"></interceptor-ref> <interceptor-ref name="loginCheck"></interceptor-ref> </interceptor-stack> </interceptors>
    <action name="user_*" class="com.xinzhi.action.UserAction" method="{1}" >
           <!-- 执行拦截器,写在action外面是拦截所有资源,写在里面是针对action内部资源进行拦截 --> <interceptor-ref name="login_interceptor"></interceptor-ref>
           <!-- 获取的拦截器返回值及跳转页面 --> <result name="loginDefaulUser">/login.jsp</result>

    <result name="loginUser" type="redirectAction">user_list</result> <result name="listUser">/WEB-INF/list.jsp</result> </action> </package> </struts>

    自己写的一个拦截器的类要继承AbstractInterceptor

    package com.xinzhi.interceptor;
    
    import com.opensymphony.xwork2.ActionContext;
    import com.opensymphony.xwork2.ActionInvocation;
    import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
    
    public class MyInterceptor extends AbstractInterceptor {
    
        @Override
        public String intercept(ActionInvocation invocation) throws Exception {
            ActionContext invocationContext = invocation.getInvocationContext();//拿到ActionContext
            String method = invocation.getProxy().getMethod();//调用代理获取方法名称
            if (!"login".equals(method)) {
                Object object = invocationContext.getSession().get("userInfo");
                if (object != null) {
                    return invocation.invoke();//执行方法
                } else {
                    return "loginDefaulUser";
                }
            } else {
                return invocation.invoke();
            }
    
        }
    
    }
  • 相关阅读:
    让用户舒服起来 10个改善UI的技术
    Powerpoint快捷键大全
    自制Flash FLV视频播放器
    Firefox与IE在CSS样式表中的差异
    让你每天都充满积极性的五个方法
    asp.net 2实用技术汇总
    春季要健康 “排毒”三步走
    皮肤变好必遵守洗脸九法
    经典博客收集
    教你一招让网页用上漂亮的11PX中文字体
  • 原文地址:https://www.cnblogs.com/ShaoXin/p/7008292.html
Copyright © 2011-2022 走看看