Extjs springmvc session 超时 处理

如果你的项目使用ExtJS作为表现层,你会发现,SESSION超时控制将是一个问题。
本文将就自己的经验,来解决这一问题,当然,解决问题并非只有一种方法,我只是提出我的方法。
首先,做超时控制,必需使用过滤器,而我们既然使用了Spring MVC,那就用拦截器取代吧,写一个拦截器,用来拦截用户请求,当然,这个拦截器还需要可以配置哪些请求是不需要拦截的。
[java] view plaincopy
/**
*
*/
package net.bioslink.business.intercepter;

import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.bioslink.common.util.ConfigureUtil;
import net.bioslink.common.vo.Constants;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.ArrayUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Repository;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

/**
* @author leoly
*
*/
@Repository
public class SystemAccessInterceper extends HandlerInterceptorAdapter {
private final Logger logger = Logger.getLogger(getClass());

@Override
public boolean preHandle(HttpServletRequest request,
HttpServletResponse response, Object handler) throws Exception {
String uri = request.getRequestURI();
String[] noFilters = ConfigureUtil.getStringArray("no_filter_setting");
boolean isFilter = true;
if (!ArrayUtils.isEmpty(noFilters)) {
for (String u : noFilters) {
if (uri.contains(u)) {
isFilter = false;
break;
}
}
}

if (isFilter) {
// Session
Object obj = request.getSession()
.getAttribute(Constants.SESSION_ID);
if (null == obj) {
logger.info("登录超时!!");
PrintWriter writer = response.getWriter();
writer.print("<script>top.location=‘http://127.0.0.1:8080/VkDesktop/login/loginSystem.do‘;</script>SESSION_TIMEOUT_ERROR");
IOUtils.closeQuietly(writer);
return false;
} else {
request.setAttribute("LOG_ACCESS_TIME",
System.currentTimeMillis());
logger.info(obj + "访问了" + uri);
}
}
return super.preHandle(request, response, handler);
}

@Override
public void afterCompletion(HttpServletRequest request,
HttpServletResponse response, Object handler, Exception ex)
throws Exception {
// TODO Auto-generated method stub
super.afterCompletion(request, response, handler, ex);
Object obj = request.getAttribute("LOG_ACCESS_TIME");
if (null != obj) {
long accessTime = (long) obj;
logger.info("处理请求" + request.getRequestURI() + "耗时"
+ (System.currentTimeMillis() - accessTime) + "毫秒!");
}
}
}

然后,将这个拦截器注册到Spring MVC中,在xxx-servlet.xml文件中添加注册代码:
[html] view plaincopy
<mvc:interceptors>
<bean class="net.bioslink.business.intercepter.SystemAccessInterceper" />
</mvc:interceptors>

OK,现在这个拦截器已经开始工作了,它会拦截用户请求,判断SESSION中是否存在登录信息,如果不存在,则说明已经超时,拦截器向客户问写一段代码
[java] view plaincopy
writer.print("<script>top.location=‘http://127.0.0.1:8080/VkDesktop/login/loginSystem.do‘;</script>SESSION_TIMEOUT_ERROR");
这样,如果客户端是网页,它就会自动跳到登录页面,如果不是网页(如AJAX请求),我们需要在客户端中判断返回的字符串中是否包含“SESSION_TIMEOUT_ERROR”,如果有的话就做出跳到首页的处理。

那么,现在轮到客户端了,因为使用的是ExtJS,几乎所有的请求都是AJAX请求,所以判断SESSION_TIMEOUT_ERROR是关键。那总不能所有的AJAX请求都加上这些判断吧??OMG,杀了我吧,可怜的码农……
其实,我们可以写一个TimeoutControl类,封装一些我们需要修改和添加的东西,来实现超时跳转功能!谁叫ExtJS提供了这么多类功能(继承,重写,覆盖)呢?
我们知道,ExtJS的请求虽然都是AJAX请求,但是却可以区分为Store请求和一般的AJAX请求,那么我们的TimeoutControl类就需要重新构造这些请求方式,并且,以后写的所有请求都需要使用这个类来完成。
[javascript] view plaincopy
Ext.define(‘Ext.ux.TimeoutControl‘, {
extend : ‘Ext.data.Store‘,
alias : ‘widget.timeoutstore‘,
constructor : function(config) {
Ext.apply(config.proxy, {
listeners : {
exception : function(self, response) {
var responseText = response.responseText;
if (responseText
&& responseText.indexOf("SESSION_TIMEOUT_ERROR") > 0) {
top.location = ‘../login/loginSystem.do‘;
}
}
}
});
this.callParent([config]);
},

statics : {
request : function(config) {
var f = config.success;
config.success = Ext.Function.createInterceptor(f, function(
response) {
var txt = response.responseText;
// alert(txt);
if (txt && txt.indexOf("SESSION_TIMEOUT_ERROR") > 0) {
top.location = ‘../login/loginSystem.do‘;
return false;
}

return true;
});
Ext.Ajax.request(config);
}
}
});

这个类中,我们继承了Ext.data.Store,并且在构造器中动态加入了一个proxy的exception处理函数,也就是说,只要使用此类,无论Store的proxy中写没写exception函数,都会在这里添加上,而这个函数就是处理超时控制的。然后,这个类还提供了一个静态方法request,这个request方法会调用Ext.Ajax.request(config),但是在调用之前,我们给配置的success函数添加了一段判断超时的代码,使用的是ExtJS的函数拦截方法,效果杠杠的。
最后,只要我们在使用到Store的地方中使用Ext.ux.TimeoutControl类,在使用Ext.Ajax.request的地方中使用Ext.ux.TimeoutControl.request取代,那么,超时控制就完成了。
使用Store的例子:
[javascript] view plaincopy
var store = Ext.create(‘Ext.ux.<span style="font-size:18px; white-space: pre; background-color: rgb(240, 240, 240);">TimeoutControl</span>‘, {
model : ‘MyDesktop.data.WorkOrderModule‘,
remoteSort : true,
remoteFilter : true,
sorters : [new Ext.util.Sorter({
property : ‘reportId‘,
direction : ‘ASC‘
})],
proxy : {
type : ‘ajax‘,
actionMethods : {
read : ‘POST‘
},
extraParams : {
projectCode : "44190008",
orderMonth : 2,
id : 0
},
pageParam : ‘pageNo‘,
limitParam : ‘pageSize‘,
url : ‘../lebang/workOrder/queryWorkorderList.do‘,
reader : {
type : ‘json‘,
root : ‘orders‘,
totalProperty : ‘totalCount‘
}
}
});

使用AJAX请求的例子:
[javascript] view plaincopy
Ext.ux.TimeoutControl.request({
url : "../user/logout.do",
timeout : 60000,
method : ‘POST‘,
disableCaching : false,
params : {},
success : function(response) {
window.location = ‘../login/loginSystem.do‘;
},
failure : function() {
Ext.Msg.alert("系统提示", "注销异常,请联系管理人员!!");
}
});

时间: 2024-07-29 19:02:36

Extjs springmvc session 超时 处理的相关文章

webix+springmvc session超时跳转登录页面

2016-10-30 13:11:56 引言 最近做项目,发现ajax请求不能在服务器中直接重定向到登录页面.查了些资料发现jquery的ajax请求有人给出了方法.但是webix的ajax请求和jquery的有些区别.这里模仿jquery的处理方式实现webix的ajax请求session超时跳转. 具体的做法: 1.查看webix.js源码发现webix.ajax只有请求前的监听函数 "onBeforeAjax", 要做到获取返回状态跳转登录页面必须要有个返回的监听函数,但是源码没

spring mvc session超时,处理ajax请求

做web开发时,当session超时时,如果不是ajax请求,很简单就能实现跳到指定的页面.但是ajax请求就会有问题.session超时的时候,点击到ajax请求就会弹出一些页面源码文件. 首先建了个拦截器,来判断session超时.用户登录后会保存用户信息在一个session里,在session的监听里,session超时会销毁保存在session里的用户信息,而拦截器就通过session里是否有用户信息来判断session超时. 拦截器是spring-mvc的拦截器,在拦截器里判断是不是a

Java设置session超时(失效)的三种方式

1.      在web容器中设置(此处以tomcat为例) 在E:\apache-tomcat-7.0.54\apache-tomcat-7.0.54\conf\web.xml中设置,以下是apache-tomcat-7.0.54中的默认配置 <!-- ==================== Default Session Configuration ================= --> <!-- You can set the default session timeout 

jQuery和ExtJS的timeOut超时问题和event事件处理问题

对jQuery来说,超时可以直接设置timeout参数,并在error事件中捕获第二个参数,如果是“timeout”则表明捕获了超时事件,非常清楚. 例子: $.ajax({         type: "POST",         contentType: "application/json",         url: "../ws/MyService.asmx/test",         data: '{"email"

PHP jquery session超时处理

php: <?php header('sessionStatus:timeOut'); //is_ajax echo $_SERVER['HTTP_X_REQUESTED_WITH']; html:<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <script type="text/javascript" sr

ajax提交session超时跳转页面使用全局的方法来处理

来自:http://www.jb51.net/article/43770.htm 如果是ajax提交,超时时从服务器发出的跳转命令就不会起作用,所以如果是session超时,而且是在ajax请求,就在响应头里,再用一个全局的方法来处理session超时要跳转的页面. 在过滤器中写入如下方法:(未测试) public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws I

Spring mvc Interceptor 解决Session超时配置流程

最近公司内部框架中对Session超时这一功能未实现,由于采用iframe结构,Session超时后,当点击左侧系统菜单时,会在iframe的右侧再次弹出登陆框. 该问题是由于没有设置拦截器造成. 添加拦截器思路:当Session超时后,用户点击menu时,需要用Interceptor进行前项拦截,并判断此时session中是否还存在用户信息,如果不存在,将其指定登陆主页面. 如下代码: 1)首先在applicationContext-mvc.xml中加入mvc:interceptor标签. <

esayui session 超时跳转登录页面

 代码部分: ① web.XML部分配置代码 <!-- filter 权限认证 只对amf请求过滤 --><filter> <filter-name>authFilter</filter-name> <filter-class>com.lactec.framework.ac.filter.AuthFilter</filter-class></filter> <filter-mapping> <filter

ajax session超时处理

1.问题:  客户端发出了一个ajax请求,但是此时session超时了 2.解决方法: 服务端: 在过滤器或者拦截器中,判断ajax请求类型,设置一个超时标志 客户端:ajax请求要求有一个全局处理函数 ,获取相应标志,做出相应处理   过滤器中加入: // 如果是ajax请求响应头会有,x-requested-with: if (req.getHeader("x-requested-with") != null && req.getHeader("x-re