Struts2自定义拦截器实例—Session超时的处理
2010-04-23 14:04
525 查看
版本:struts2.1.6
实例功能:当用户登陆后,session超时后则返回到登陆页面重新登陆。
为了更好的实现此功能我们先将session失效时间设置的小点,这里我们设置成1分钟
修改web.xml
此实例用到的jsp页面及登陆所涉及到的相关代码请参考:
Struts2自定义拦截器实例—登陆权限验证
实现自定义拦截器类
struts.xml中定义并使用此拦截器
当我们登陆后一分钟不做任何操作刷新后则会跳转到登陆页面
实例功能:当用户登陆后,session超时后则返回到登陆页面重新登陆。
为了更好的实现此功能我们先将session失效时间设置的小点,这里我们设置成1分钟
修改web.xml
<session-config> <session-timeout>1</session-timeout> </session-config>
此实例用到的jsp页面及登陆所涉及到的相关代码请参考:
Struts2自定义拦截器实例—登陆权限验证
实现自定义拦截器类
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中定义并使用此拦截器
<?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>
当我们登陆后一分钟不做任何操作刷新后则会跳转到登陆页面
相关文章推荐
- Struts2自定义拦截器实例—Session超时的处理
- Struts2自定义拦截器实例—Session超时的处理
- Struts2自定义拦截器实例—Session超时的处理
- struts2自定义拦截器 模拟session超时的处理
- Struts2自定义拦截器—Session超时的处理
- struts2自定义拦截器二——模拟session超时的处理
- Struts2自定义拦截器—Session超时的处理
- dwz ajax session超时跳转登录页(struts2自定义拦截器)
- Struts2自定义拦截器实例—登陆权限验证
- struts2中自定义用户session失效拦截器
- struts2声明式异常处理,友好的错误界面,自定义异常拦截器,
- Struts2 自定义拦截器实例—登陆权限验证
- Struts2 自定义拦截器实例—登陆权限验证
- Struts2的异常处理机制、自定义拦截器
- Struts2自定义拦截器实例—只允许从登录页面进入系统
- Struts2自定义拦截器实例—登陆权限验证
- Struts2自定义拦截器实例—登陆权限验证
- Struts2 拦截器 及如何获得 servlet 请求对象 以及Struts 基本配置 &&Session 超时设置
- Struts2自定义拦截器实例—登陆权限验证
- Struts2自定义拦截器实例—登陆权限验证