Struts2自定义拦截器实例—Session超时的处理

版本:struts2.1.6

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

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

修改web.xml

[c-sharp] view plain copy
  1.  <session-config>  
  2. <session-timeout>1</session-timeout>  
  3. </session-config>  

 

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

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

 

实现自定义拦截器类

[c-sharp] view plain copy
  1. package com.ywjava.interceptor;  
  2.   
  3. import java.util.Map;  
  4.   
  5. import com.opensymphony.xwork2.Action;  
  6. import com.opensymphony.xwork2.ActionContext;  
  7. import com.opensymphony.xwork2.ActionInvocation;  
  8. import com.opensymphony.xwork2.interceptor.AbstractInterceptor;  
  9. import com.ywjava.action.LoginAction;  
  10. import com.ywjava.utils.Constants;  
  11.   
  12. public class SessionIterceptor extends AbstractInterceptor {  
  13.   
  14.     @Override  
  15.     public String intercept(ActionInvocation actionInvocation) throws Exception {  
  16.         ActionContext ctx = ActionContext.getContext();  
  17.         Map session = ctx.getSession();  
  18.         Action action = (Action) actionInvocation.getAction();  
  19.         if (action instanceof LoginAction) {  
  20.             return actionInvocation.invoke();  
  21.         }  
  22.         String userName = (String) session.get(Constants.USER_SESSION);  
  23.         if (userName == null) {  
  24.             return Action.LOGIN;  
  25.         } else {  
  26.             return actionInvocation.invoke();  
  27.         }  
  28.     }  
  29.   
  30. }  

 

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

[c-sharp] view plain copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE struts PUBLIC  
  3.     "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"  
  4.     "http://struts.apache.org/dtds/struts-2.1.dtd">  
  5. <struts>  
  6.     <package name="authority" extends="struts-default">  
  7.       
  8.         <!-- 定义一个拦截器 -->  
  9.         <interceptors>  
  10.             <interceptor name="authority"  
  11.                 class="com.ywjava.interceptor.LoginInterceptor">  
  12.             </interceptor>  
  13.             <interceptor name="sessionout"  
  14.              class="com.ywjava.interceptor.SessionIterceptor"></interceptor>  
  15.             <!-- 拦截器栈 -->  
  16.             <interceptor-stack name="mydefault">  
  17.                 <interceptor-ref name="defaultStack" />  
  18.                 <interceptor-ref name="authority" />  
  19.                 <interceptor-ref name="sessionout"/>  
  20.             </interceptor-stack>  
  21.         </interceptors>  
  22.   
  23.         <!-- 定义全局Result -->  
  24.         <global-results>  
  25.             <!-- 当返回login视图名时,转入/login.jsp页面 -->  
  26.             <result name="login">/login.jsp</result>  
  27.         </global-results>  
  28.   
  29.         <action name="loginform"  
  30.             class="com.ywjava.action.LoginFormAction">  
  31.             <result name="success">/login.jsp</result>  
  32.         </action>  
  33.           
  34.         <action name="login" class="com.ywjava.action.LoginAction">  
  35.             <result name="success">/welcome.jsp</result>  
  36.             <result name="error">/login.jsp</result>  
  37.             <result name="input">/login.jsp</result>  
  38.         </action>  
  39.   
  40.         <action name="show" class="com.ywjava.action.ShowAction">  
  41.             <result name="success">/show.jsp</result>  
  42.             <!-- 使用此拦截器 -->  
  43.             <interceptor-ref name="mydefault" />  
  44.         </action>  
  45.           
  46.     </package>  
  47. </struts>  

 

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




转载自:http://blog.csdn.net/java_cxrs/article/details/5519743

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值