• struts2拦截器使用(用户session失效后的统一页面指向) Mr


    用户登录之后session我们设置一定的时间失效,失效后页面的转向问题一般都是跳回主页或者是我们自定义的页面

    action:

    package com.tkbs.web.interceptors;

    import java.util.Date;
    import java.util.Map;

    import javax.annotation.Resource;

    import com.opensymphony.xwork2.Action;
    import com.opensymphony.xwork2.ActionContext;
    import com.opensymphony.xwork2.ActionInvocation;
    import com.opensymphony.xwork2.interceptor.Interceptor;
    import com.tkbs.domain.user.UserAccounts;
    import com.tkbs.service.user.UserService;

    /**
    * 用户拦截器
    * author zxg
    * version 1.0
    */
    publicclass UserInterceptor implements Interceptor {

    privatestaticfinallong serialVersionUID =5582842221490358379L;

    publicvoid destroy() {

    }

    /**
    * 在服务启动的时候执行
    */
    publicvoid init() {
    }

    public String intercept(ActionInvocation actionInvocation) throws Exception {
    System.out.println(
    "<<<教师拦截器初始化...");

    ActionContext ctx
    = ActionContext.getContext();
    Map session
    = ctx.getSession();
    // sessionUser 如果session中不存在用户在返回index视图
    if (session.get("sessionUser")==null) {
    return"index";
    }
    String result
    = actionInvocation.invoke();
    return result;
    }
    }

    如果注册页面的请求也包含在拦截器的拦截中加以判断:

    package com.tkbs.web.interceptors;

    import java.util.Date;
    import java.util.Map;

    import javax.annotation.Resource;
    import javax.servlet.http.HttpServletRequest;

    import org.apache.struts2.ServletActionContext;

    import com.opensymphony.xwork2.Action;
    import com.opensymphony.xwork2.ActionContext;
    import com.opensymphony.xwork2.ActionInvocation;
    import com.opensymphony.xwork2.interceptor.Interceptor;
    import com.tkbs.domain.user.UserAccounts;
    import com.tkbs.service.user.UserService;

    /**
    * 用户拦截器
    * author zxg
    * version 1.0
    */

    publicclass UserInterceptor implements Interceptor {

    privatestaticfinallong serialVersionUID =5582842221490358379L;

    publicvoid destroy() {

    }

    /**
    * 在服务启动的时候执行
    */
    publicvoid init() {
    }

    public String intercept(ActionInvocation actionInvocation) throws Exception {
    System.out.println(
    "<<<教师拦截器初始化...");

    ActionContext ctx
    = ActionContext.getContext();
    Map session
    = ctx.getSession();
    // sessionStudent
    if (session.get("sessionUser")==null) {

    //判断是不是注册用户,如果是注册用户(包含userRegisterPage)则继续执行,否则跳转到index视图
    HttpServletRequest request = (HttpServletRequest)ctx.get(ServletActionContext.HTTP_REQUEST);
    String uri
    = request.getRequestURI();
    if(uri.indexOf("userRegistPage") ==-1) {
    return"index";
    }
    }
    String result
    = actionInvocation.invoke();
    return result;
    }
    }

    这里用户的拦截器定义在user包下

    User包下拦截器
    <!-- user 包配置 -->
    <package name="user" extends="myDefault" namespace="/user">
    <interceptors>
    <interceptor name="userInterceptor"
    class
    ="com.tkbs.web.interceptors.UserInterceptor">
    </interceptor>
    <interceptor-stack name="userStack">
    <interceptor-ref name="userInterceptor"></interceptor-ref>
    <interceptor-ref name="myDefaultStack"></interceptor-ref>
    </interceptor-stack>
    </interceptors>
    <!-- 默认拦截器,此包下所有的ACTION将都被拦截。如果ACTION再定义了拦截器,则失效 -->
    <default-interceptor-ref name="userStack"></default-interceptor-ref>
    <global-results>
    <!-- 首页-->
    <result name="index">/index.jsp</result>
    </global-results>
    <!-- *_*_*
    页面转向${ctp}/page/resource_queryResource_resourceList
    ...actions.page.resource.queryResource[method=resourceList]
    ...actions.page.resource.queryResourceDetial[method=resourceDetial]
    -->
    <action name="*_*_*" class="{2}Action" method="{3}">
    <result name="update" type="redirectAction">
    <param name="actionName">
    userDiary_userDiary_diaryShow
    </param>
    <param name="namespace">/user</param>
    <param name="parse">true</param>
    <param name="diaryId">${diaryId}</param>
    <param name="userId">${userId}</param>
    </result>
    <!-- page 与包相应 -->
    <result name="{3}" type="dispatcher">
    /modulePage/{1}/{3}.jsp
    </result>
    <!-- json -->
    <result name="json" type="json"/>
    <!-- user -->
    <result name="pwdUpOff" type="redirectAction">
    <param name="namespace">/user</param>
    <param name="actionName">
    userUser_user_userIndex
    </param>
    <param name="parse">true</param>
    </result>
    </action>
    </package>

    全局拦截器的定义:

    全局拦截器:
    <!-- default (全局)包配置 -->
    <package name="myDefault" extends="json-default" namespace="/">
    <interceptors>
    <interceptor name="configInterceptor"
    class
    ="com.tkbs.web.interceptors.ConfigInterceptor">
    </interceptor>
    <!-- 默认的必须放在最下面! -->
    <interceptor-stack name="myDefaultStack">
    <interceptor-ref name="configInterceptor"></interceptor-ref>
    <interceptor-ref name="defaultStack"></interceptor-ref>
    </interceptor-stack>
    </interceptors>
    <!-- 默认拦截器,此包下所有的ACTION将都被拦截。如果ACTION再定义了拦截器,则失效 -->
    <default-interceptor-ref name="myDefaultStack"></default-interceptor-ref>
    <global-results>
    <!-- 首页-->
    <result name="index">/index.jsp</result>
    <result name="adminLoginPage">
    /moduleAdmin/index/adminLogin.jsp
    </result>
    </global-results>
    <!-- 用户登录 login_adminLogin/userLogin-->
    <action name="login_*" class="loginAction" method="{1}">
    <!-- 前台用户登录 userLogin-->
    <result name="loginMessage" type="json"></result>
    <result name="adminLogSuccess" type="redirectAction">
    <param name="actionName">
    index_indexManage_adminIndex
    </param>
    <param name="namespace">/admin</param>
    </result>
    <result name="adminLoginIn">
    /moduleAdmin/index/adminIndex.jsp
    </result>
    <result name="adminLoginPage">
    /moduleAdmin/index/adminLogin.jsp
    </result>
    </action>
    <!-- 用户退出登录-->
    <action name="existLogin_*" class="existLoginAction"
    method
    ="{1}">
    <result name="index">/index.jsp</result>
    </action>
    </package>

    Struts2分别对Request、Session和Application三个对象封装成了三个Map对象,直接使用封装好的Map对象来读取和保存数据。可以通过com.opensymphony.xwork2.ActionContext类来得到这三个对象。ActionContext是Action执行的上下文,保存了很多对象如parameters、request、session、application和locale等。通过ActionContext类获取Map对象的方法为:

    ActionContext context=ActionContext.getContext(); --得到Action执行的上下文

    Map request=(Map)context.get("request");--得到HttpServletRequest的Map对象

    Map session=context.getSession();--得到HttpSession的Map对象

    Map application=context.getApplication();--得到ServletContext的Map对象

    HttpServletRequest request = (HttpServletRequest)context.get(ServletActionContext.HTTP_REQUEST);

    HttpServletRequest request = ctx.get(org.apache.struts2.StrutsStatics.HTTP_REQUEST);

    web.xml配置

    <!-- session超时定义,单位为分钟 -->
    <session-config>
    <session-timeout>20</session-timeout>
    </session-config>
    <!-- struts2 -->
    <filter>
    <filter-name>struts2</filter-name>
    <filter-class>
    <!--org.apache.struts2.dispatcher.FilterDispatcher-->
    org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
    </filter-class>
    </filter>
    <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
    </filter-mapping>
    <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>*.action</url-pattern>
    </filter-mapping>
    <!-- 配置struts2的CleanUp的Filter -->
    <filter>
    <filter-name>struts-cleanup</filter-name>
    <filter-class>
    org.apache.struts2.dispatcher.ActionContextCleanUp
    </filter-class>
    </filter>
    <filter-mapping>
    <filter-name>struts-cleanup</filter-name>
    <url-pattern>/*</url-pattern>
    Mr-sniper
    北京市海淀区
    邮箱:rafx_z@hotmail.com
  • 相关阅读:
    C#图片处理示例(裁剪,缩放,清晰度,水印)
    lucene4.5近实时搜索
    mongo 多条件 查询
    Lucene:QueryParser
    Lucene的中文分词器IKAnalyzer
    Lucene为不同字段指定不同分词器(转)
    Thrift初用小结
    lucene4.0与之前版本的一些改变
    lucene 资料
    Mongodb快速入门之使用Java操作Mongodb
  • 原文地址:https://www.cnblogs.com/rafx/p/interceptor.html
Copyright © 2020-2023  润新知