前段时间,就搜到一篇介绍如何控制EXTJS中SESSION超时的文章,偏就没有一次搞定,现把自己整理的经验整理出来,代码如下:
第一步:
自己定义一个Filter,然后在doFilter()中加如下代码:
HttpSession session = request.getSession(false); if (session == null || session.getAttribute("user") == null) { if (request.getHeader("x-requested-with") != null && request.getHeader("x-requested-with").equalsIgnoreCase( "XMLHttpRequest")) { response.setHeader("sessionstatus", "timeout"); } }
再在WEB.XML文件中配置这个Filter,并且要放在STRUTS2的StrutsPrepareAndExecuteFilter前面,相关代码:
<filter> <filter-name>AuthFilter</filter-name> <filter-class>com.vectorsoft.util.SessionCheckFilter</filter-class> </filter> <filter-mapping> <filter-name>AuthFilter</filter-name> <url-pattern>*.jsp</url-pattern> </filter-mapping>
第二步:
在每个功能JS中添加代码:
Ext.onReady(function(){ Ext.QuickTips.init(); Ext.Ajax.on('requestcomplete',checkUserSessionStatus, this); function checkUserSessionStatus(conn,response,options){ var sessionStatus = response.getResponseHeader("sessionstatus"); if(typeof(sessionStatus) != "undefined"){ Ext.Msg.alert('提示', '会话超时,请重新登录!', function(btn, text){ if (btn == 'ok'){ var redirect = '../LogoutAction'; window.location = redirect; } }); } }; //其它代码...... }
在完成上面两步后,基本上就已经实现了EXTJS的SESSION的超时控制,并可以顺利让用户转向登录页.