最近做項目,發(fā)現(xiàn)ajax請求不能在服務(wù)器中直接重定向到登錄頁面。查了些資料發(fā)現(xiàn)jquery的ajax請求有人給出了方法。但是webix的ajax請求和jquery的有些區(qū)別。這里模仿jquery的處理方式實現(xiàn)webix的ajax請求session超時跳轉(zhuǎn)。
具體的做法:
1、查看webix.js源碼發(fā)現(xiàn)webix.ajax只有請求前的監(jiān)聽函數(shù) "onBeforeAjax", 要做到獲取返回狀態(tài)跳轉(zhuǎn)登錄頁面必須要有個返回的監(jiān)聽函數(shù),但是源碼沒有。所以我修改了下源碼,加了個返回的監(jiān)聽函數(shù)"onAfterAjax"。
紅色標(biāo)記部分是我加的代碼,當(dāng)檢測到ajax完成時,自動執(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)識是x-requested-with ,所以我模擬給了個標(biāo)識requestFlag="webix"(可以自己設(shè)置個喜歡的),用"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、后臺代碼
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)頭會有,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控制臺執(zhí)行下webix.ajax查看效果
webix.ajax().get("/webix/mvc/login.action")
以上所述是小編給大家介紹的webix+springmvc session超時跳轉(zhuǎn)登錄頁面,希望對大家有所幫助,如果大家有任何疑問歡迎給我留言,小編會及時回復(fù)大家的。
更多webix+springmvc session超時跳轉(zhuǎn)登錄頁面相關(guān)文章請關(guān)注PHP中文網(wǎng)!
聲明:本網(wǎng)頁內(nèi)容旨在傳播知識,若有侵權(quán)等問題請及時與本網(wǎng)聯(lián)系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com