zoukankan      html  css  js  c++  java
  • struts2自定义拦截器 设置session并跳转

    实例功能:当用户登陆后,session超时后则返回到登陆页面重新登陆。

    为了更好的实现此功能我们先将session失效时间设置的小点,这里我们设置成1分钟

    修改web.xml

    view plaincopy to clipboardprint?
     <session-config>  
    <session-timeout>1</session-timeout>  
    </session-config> 
      <session-config>
     <session-timeout>1</session-timeout>
     </session-config>

    此实例用到的jsp页面及登陆所涉及到的相关代码请参考:

    Struts2自定义拦截器实例—登陆权限验证

    实现自定义拦截器类

    view plaincopy to clipboardprint?
    package com.ywjava.interceptor;  
     
    import java.util.Map;  
     
    import com.opensymphony.xwork2.Action;  
    import com.opensymphony.xwork2.ActionContext;  
    import com.opensymphony.xwork2.ActionInvocation;  
    import com.opensymphony.xwork2.interceptor.AbstractInterceptor;  
    import com.ywjava.action.LoginAction;  
    import com.ywjava.utils.Constants;  
     
    public class SessionIterceptor extends AbstractInterceptor {  
     
        @Override  
        public String intercept(ActionInvocation actionInvocation) throws Exception {  
            ActionContext ctx = ActionContext.getContext();  
            Map session = ctx.getSession();  
            Action action = (Action) actionInvocation.getAction();  
            if (action instanceof LoginAction) {  
                return actionInvocation.invoke();  
            }  
            String userName = (String) session.get(Constants.USER_SESSION);  
            if (userName == null) {  
                return Action.LOGIN;  
            } else {  
                return actionInvocation.invoke();  
            }  
        }  
     

    package com.ywjava.interceptor;

    import java.util.Map;

    import com.opensymphony.xwork2.Action;
    import com.opensymphony.xwork2.ActionContext;
    import com.opensymphony.xwork2.ActionInvocation;
    import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
    import com.ywjava.action.LoginAction;
    import com.ywjava.utils.Constants;

    public class SessionIterceptor extends AbstractInterceptor {

     @Override
     public String intercept(ActionInvocation actionInvocation) throws Exception {
      ActionContext ctx = ActionContext.getContext();
      Map session = ctx.getSession();
      Action action = (Action) actionInvocation.getAction();
      if (action instanceof LoginAction) {
       return actionInvocation.invoke();
      }
      String userName = (String) session.get(Constants.USER_SESSION);
      if (userName == null) {
       return Action.LOGIN;
      } else {
       return actionInvocation.invoke();
      }
     }

    }
     

    struts.xml中定义并使用此拦截器

    view plaincopy to clipboardprint?
    <?xml version="1.0" encoding="UTF-8"?>  
    <!DOCTYPE struts PUBLIC  
        "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" 
        "http://struts.apache.org/dtds/struts-2.1.dtd">  
    <struts>  
        <package name="authority" extends="struts-default">  
          
            <!-- 定义一个拦截器 -->  
            <interceptors>  
                <interceptor name="authority" 
                    class="com.ywjava.interceptor.LoginInterceptor">  
                </interceptor>  
                <interceptor name="sessionout" 
                 class="com.ywjava.interceptor.SessionIterceptor"></interceptor>  
                <!-- 拦截器栈 -->  
                <interceptor-stack name="mydefault">  
                    <interceptor-ref name="defaultStack" />  
                    <interceptor-ref name="authority" />  
                    <interceptor-ref name="sessionout"/>  
                </interceptor-stack>  
            </interceptors>  
     
            <!-- 定义全局Result -->  
            <global-results>  
                <!-- 当返回login视图名时,转入/login.jsp页面 -->  
                <result name="login">/login.jsp</result>  
            </global-results>  
     
            <action name="loginform" 
                class="com.ywjava.action.LoginFormAction">  
                <result name="success">/login.jsp</result>  
            </action>  
              
            <action name="login" class="com.ywjava.action.LoginAction">  
                <result name="success">/welcome.jsp</result>  
                <result name="error">/login.jsp</result>  
                <result name="input">/login.jsp</result>  
            </action>  
     
            <action name="show" class="com.ywjava.action.ShowAction">  
                <result name="success">/show.jsp</result>  
                <!-- 使用此拦截器 -->  
                <interceptor-ref name="mydefault" />  
            </action>  
              
        </package>  
    </struts> 
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE struts PUBLIC
     "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"
     "http://struts.apache.org/dtds/struts-2.1.dtd">
    <struts>
     <package name="authority" extends="struts-default">
     
      <!-- 定义一个拦截器 -->
      <interceptors>
       <interceptor name="authority"
        class="com.ywjava.interceptor.LoginInterceptor">
       </interceptor>
       <interceptor name="sessionout"
        class="com.ywjava.interceptor.SessionIterceptor"></interceptor>
       <!-- 拦截器栈 -->
       <interceptor-stack name="mydefault">
        <interceptor-ref name="defaultStack" />
        <interceptor-ref name="authority" />
        <interceptor-ref name="sessionout"/>
       </interceptor-stack>
      </interceptors>

      <!-- 定义全局Result -->
      <global-results>
       <!-- 当返回login视图名时,转入/login.jsp页面 -->
       <result name="login">/login.jsp</result>
      </global-results>

      <action name="loginform"
       class="com.ywjava.action.LoginFormAction">
       <result name="success">/login.jsp</result>
      </action>
      
      <action name="login" class="com.ywjava.action.LoginAction">
       <result name="success">/welcome.jsp</result>
       <result name="error">/login.jsp</result>
       <result name="input">/login.jsp</result>
      </action>

      <action name="show" class="com.ywjava.action.ShowAction">
       <result name="success">/show.jsp</result>
       <!-- 使用此拦截器 -->
       <interceptor-ref name="mydefault" />
      </action>
      
     </package>
    </struts>

    当我们登陆后一分钟不做任何操作刷新后则会跳转到登陆页面

  • 相关阅读:
    SPI Flash(W25Q16DV) 驱动
    SPI Flash(W25Q16DV) 基本操作
    SPI OLED 驱动
    dategrip破解
    mysql中find_in_set()函数的使用
    MYSQL中,CAST函数的使用规则
    关于Java对象作为参数传递是传值还是传引用的问题
    easyPOI导出excel报错
    ### Cause: java.lang.reflect.UndeclaredThrowableException
    用count(*)还是count(列名) || Mysql中的count()与sum()区别
  • 原文地址:https://www.cnblogs.com/xuehen/p/4281891.html
Copyright © 2011-2022 走看看