webix+springmvc session超時(shí)跳轉(zhuǎn)登錄頁面
引言
最近做項(xiàng)目,發(fā)現(xiàn)ajax請求不能在服務(wù)器中直接重定向到登錄頁面。查了些資料發(fā)現(xiàn)jquery的ajax請求有人給出了方法。但是webix的ajax請求和jquery的有些區(qū)別。這里模仿jquery的處理方式實(shí)現(xiàn)webix的ajax請求session超時(shí)跳轉(zhuǎn)。
具體的做法:
1、查看webix.js源碼發(fā)現(xiàn)webix.ajax只有請求前的監(jiān)聽函數(shù) "onBeforeAjax", 要做到獲取返回狀態(tài)跳轉(zhuǎn)登錄頁面必須要有個(gè)返回的監(jiān)聽函數(shù),但是源碼沒有。所以我修改了下源碼,加了個(gè)返回的監(jiān)聽函數(shù)"onAfterAjax"。
紅色標(biāo)記部分是我加的代碼,當(dāng)檢測到ajax完成時(shí),自動(dòng)執(zhí)行"onAfterAjax"。(代碼的位置可以搜索webix.js ,條件"onBeforeAjax",然后在對應(yīng)的位置加入紅色代碼就行
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請求沒有明顯的標(biāo)志,jquery.ajax的標(biāo)識(shí)是x-requested-with ,所以我模擬給了個(gè)標(biāo)識(shí)requestFlag="webix"(可以自己設(shè)置個(gè)喜歡的),用"onBeforeAjax"設(shè)置
webix.attachEvent("onBeforeAjax",function(s, t, e, a, o){o["requestFlag"]="webix"})
3、監(jiān)聽返回狀態(tài)
webix.attachEvent("onAfterAjax",function(xhr){if(xhr.getResponseHeader("sessionstatus")=='timeout'){window.location.href='/webix/login.html'}});
4、后臺(tái)代碼
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請求響應(yīng)頭會(huì)有,requestFlag
response.setHeader("sessionstatus", "timeout");// 在響應(yīng)頭設(shè)置session狀態(tài)
} 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控制臺(tái)執(zhí)行下webix.ajax查看效果
webix.ajax().get("/webix/mvc/login.action")
以上所述是小編給大家介紹的webix+springmvc session超時(shí)跳轉(zhuǎn)登錄頁面,希望對大家有所幫助,如果大家有任何疑問歡迎給我留言,小編會(huì)及時(shí)回復(fù)大家的。
- Java Web實(shí)現(xiàn)session過期后自動(dòng)跳轉(zhuǎn)到登陸頁功能【基于過濾器】
- 詳解springmvc控制登錄用戶session失效后跳轉(zhuǎn)登錄頁面
- php頁面跳轉(zhuǎn)session cookie丟失導(dǎo)致不能登錄等問題的解決方法
- jQuery ajax全局函數(shù)處理session過期后的ajax跳轉(zhuǎn)問題
- ajax提交session超時(shí)跳轉(zhuǎn)頁面使用全局的方法來處理
- Jsp中解決session過期跳轉(zhuǎn)到登陸頁面并跳出iframe框架的方法
- Session過期后自動(dòng)跳轉(zhuǎn)到登錄頁面的實(shí)例代碼
- Ajax Session失效跳轉(zhuǎn)登錄頁面的方法
- Session過期后實(shí)現(xiàn)自動(dòng)跳轉(zhuǎn)登錄頁面
相關(guān)文章
JS中map與forEach無法跳出循環(huán)及every和some的使用
在我們平時(shí)使用習(xí)慣中,for循環(huán)里要跳出整個(gè)循環(huán)是使用break,但在數(shù)組中用forEach循環(huán)或者map如要退出整個(gè)循環(huán)使用break會(huì)報(bào)錯(cuò),使用return也不能跳出循環(huán),下面這篇文章主要介紹了關(guān)于JS中map與forEach無法跳出循環(huán)及every和some的使用的相關(guān)資料,需要的朋友可以參考下2023-05-05
Javascript結(jié)合css實(shí)現(xiàn)網(wǎng)頁換膚功能
現(xiàn)在網(wǎng)站換皮膚是比較常見的功能,大多數(shù)論壇都有的,要想實(shí)現(xiàn)這樣效果可以看如下代碼.2009-11-11
微信小程序使用WxJava獲取用戶手機(jī)號(hào)步驟
這篇文章主要介紹了微信小程序使用WxJava獲取用戶手機(jī)號(hào)的相關(guān)資料,還詳細(xì)講解了WxMpService接口的主要功能和常用方法,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-12-12
JS生態(tài)系統(tǒng)加速模塊解析賦能性能優(yōu)化探索
這篇文章主要為大家介紹了JS生態(tài)系統(tǒng)加速模塊解析賦能性能優(yōu)化探索,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2024-01-01
JavaScript使用Base64編碼和Blob對象加密圖像url地址
有時(shí)候會(huì)看到一些網(wǎng)站的圖片src中是blob:http://example.com/7c672acb-375c-4a26-9af9-99cb4c77f04d,這樣的圖片加載怎么實(shí)現(xiàn)呢?本文講解在瀏覽器中JavaScript使用解析Base64編碼和Blob對象技術(shù)來實(shí)現(xiàn),下面是實(shí)現(xiàn)的步驟和相應(yīng)的示例代碼,2023-12-12
圖片動(dòng)畫橫條廣告帶上下滾動(dòng)可自定義圖片、鏈接等等
可以自定義廣告的圖片、鏈接、長、寬等。光標(biāo)移到圖片上會(huì)出現(xiàn)左右箭頭,感興趣的朋友可以嘗試測試下2013-10-10

