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

引言

最近做项目,发现ajax请求不能在服务器中直接重定向到登录页面。查了些资料发现jquery的ajax请求有人给出了方法。但是webix的ajax请求和jquery的有些区别。这里模仿jquery的处理方式实现webix的ajax请求session超时跳转。

具体的做法:

1、查看webix.js源码发现webix.ajax只有请求前的监听函数 "onBeforeAjax", 要做到获取返回状态跳转登录页面必须要有个返回的监听函数,但是源码没有。所以我修改了下源码,加了个返回的监听函数"onAfterAjax"。

红色标记部分是我加的代码,当检测到ajax完成时,自动执行"onAfterAjax"。(代码的位置可以搜索webix.js ,条件"onBeforeAjax",然后在对应的位置加入红色代码就行

if (webix.callEvent("onBeforeAjax", [s, t, e, a, o, null, r])) {

var h = !1;

if ("GET" !== s) {

var l = !1;

for (var c in o)"content-type" == c.toString().toLowerCase() && (l = !0, "application/json" == o[c] && (h = !0));

l || (o["Content-Type"] = "application/x-www-form-urlencoded")

}

if ("object" == typeof e)if (h)e = this.stringify(e); else {

var u = [];

for (var d in e) {

var f = e[d];

(null === f || f === webix.undefined) && (f = ""), "object" == typeof f && (f = this.stringify(f)), u.push(d + "=" + encodeURIComponent(f))

}

e = u.join("&")

}

e && "GET" === s && (t = t + (-1 != t.indexOf("?") ? "&" : "?") + e,

e = null), a.open(s, t, !this.H);

var b = this.Tw;

b && (a.responseType = b);

for (var c in o)a.setRequestHeader(c, o[c]);

var x = this;

return this.master = this.master || n, a.onreadystatechange = function () {

if (!a.readyState || 4 == a.readyState) {

if (webix.callEvent("onAfterAjax", [a]) === !1) {

return false;

};

if (webix.ajax.count++, i && x && !a.aborted) {

if (-1 != webix.ly.find(a))return webix.ly.remove(a);

var t, e, s = x.master || x, r = a.status >= 400 || 0 === a.status;

"blob" == a.responseType || "arraybuffer" == a.responseType ? (t = "", e = a.response) : (t = a.responseText || "", e = x.J(a)), webix.ajax.$callback(s, i, t, e, a, r)

}

x && (x.master = null), i = x = n = null

}

}, this.qh && (a.timeout = this.qh), this.H ? a.send(e || null) : setTimeout(function () {

a.aborted || (-1 != webix.ly.find(a) ? webix.ly.remove(a) : a.send(e || null));

}, 1), this.master && this.master.Ve && this.master.Ve.push(a), this.H ? a : r

}

2、webix.ajx请求没有明显的标志,jquery.ajax的标识是x-requested-with ,所以我模拟给了个标识requestFlag="webix"(可以自己设置个喜欢的),用"onBeforeAjax"设置

webix.attachEvent("onBeforeAjax",function(s, t, e, a, o){o["requestFlag"]="webix"})

3、监听返回状态

webix.attachEvent("onAfterAjax",function(xhr){if(xhr.getResponseHeader("sessionstatus")=='timeout'){window.location.href='/webix/login.html'}});

4、后台代码

4.1 拦截器代码

package com.ljx.filter;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.HandlerInterceptor;

import org.springframework.web.servlet.ModelAndView;

public class UserInterceptor implements HandlerInterceptor {

@Override

public void afterCompletion(HttpServletRequest arg0,

HttpServletResponse arg1, Object arg2, Exception arg3)

throws Exception {

}

@Override

public void postHandle(HttpServletRequest arg0,

HttpServletResponse response, Object arg2, ModelAndView arg3)

throws Exception {

response.sendRedirect("/webix/login.html");

}

@Override

public boolean preHandle(HttpServletRequest request,

HttpServletResponse response, Object handler) throws Exception {

Object obj = request.getSession().getAttribute("LOGIN");

if (null == obj) { // 未登录

if (request.getHeader("requestFlag") != null

&& request.getHeader("requestFlag").equalsIgnoreCase(

"webix")) { // 如果是ajax请求响应头会有,requestFlag

response.setHeader("sessionstatus", "timeout");// 在响应头设置session状态

} else {

response.sendRedirect(request.getContextPath() + "/login");

}

return false;

}

return true;

}

}

4.2 spring配置文件加入拦截器配置

<mvc:interceptors>

<mvc:interceptor>

<mvc:mapping path="/mvc/*" />

<bean class="com.ljx.filter.UserInterceptor"></bean>

</mvc:interceptor>

</mvc:interceptors>

4.3 在F12控制台执行下webix.ajax查看效果

webix.ajax().get("/webix/mvc/login.action")

以上所述是小编给大家介绍的webix+springmvc session超时跳转登录页面,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的。

以上是 webix+springmvc session超时跳转登录页面 的全部内容, 来源链接: utcz.com/z/340237.html

回到顶部