引言
最近做項目,發現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、監聽返回狀態
4.1 攔截器代碼
webix.attachEvent("onAfterAjax",function(xhr){if(xhr.getResponseHeader("sessionstatus")=='timeout'){window.location.href='/webix/login.html'}});
4.2 spring設定檔加入攔截器設定
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; } }
以上所述是小編給大家介紹的webix+springmvc session超時跳轉登入頁面,希望對大家有幫助,如果大家有任何疑問歡迎給我留言,小編會及時回覆大家的。跳轉登入頁面相關文章請追蹤PHP中文網!