WEBIX+SPRINGMVC Session Timeout Jump login page _javascript tips

Source: Internet
Author: User

Introduction

A recent project has found that Ajax requests cannot be redirected directly to the login page in the server. I checked some data and found that the AJAX request for jquery was given a method. But there are some differences between Webix Ajax requests and jquery. This simulates the way jquery is handled to implement WEBIX AJAX request session timeout jump.

Specific approach:

1, see Webix.js Source found Webix.ajax only request before the listening function "Onbeforeajax", to achieve the return status Jump login page must have a return of the listener function, but the source code does not. So I modified the source code, added a return of the listener function "Onafterajax".

The Red Flag section is the code I add, which automatically executes "Onafterajax" when the Ajax finish is detected. (The location of the code can search for webix.js, condition "Onbeforeajax", and then add the red code in the corresponding position.

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 request no obvious signs, Jquery.ajax logo is x-requested-with, so I simulate to give a logo requestflag= "Webix" (you can set a favorite), with "Onbeforeajax" Set up

Webix.attachevent ("Onbeforeajax", function (S, T, E, A, O) {o["Requestflag"]= "Webix"})

3. Monitor return status

Webix.attachevent ("Onafterajax", function (XHR) {if (Xhr.getresponseheader ("sessionstatus") = = ' Timeout ') { window.location.href= '/webix/login.html '}});

4. Background code

4.1 Interceptor Code

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 (Httpser Vletrequest 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 exc eption {Object obj = request.getsession (). getattribute ("login"); if (null = obj) {//Not logged on if (Request.getheader) ("Request
Flag ")!= null && request.getheader (" Requestflag "). Equalsignorecase (" Webix ")) {//If the AJAX request response header will be there, Requestflag Response.setheader ("SessiOnstatus "," timeout ")//Set session state in response header} else {Response.sendredirect (Request.getcontextpath () +"/login "); return FA
Lse
return true; }
}

4.2 Spring configuration file Join Interceptor Configuration

<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path= "/mvc/*"/>
<bean class= " Com.ljx.filter.UserInterceptor "></bean>
</mvc:interceptor>
</mvc:interceptors>

4.3 Webix.ajax viewing effect under the F12 console

Webix.ajax (). Get ("/webix/mvc/login.action")

The above is a small set to introduce the WEBIX+SPRINGMVC session Timeout jump login page, I hope to help you, if you have any questions welcome to my message, small series will promptly reply to everyone.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.