DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> JavaScript入門知識 >> 關於JavaScript >> webix+springmvc session超時跳轉登錄頁面
webix+springmvc session超時跳轉登錄頁面
編輯:關於JavaScript     

引言

最近做項目,發現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超時跳轉登錄頁面,希望對大家有所幫助,如果大家有任何疑問歡迎給我留言,小編會及時回復大家的。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved