1.ajaxsetup------写在公用js中(每个ajax请求之后都会走这个回调) 2.
拦截器设置回调的参数,或者请求状态,ajaxsetup回调参数做出处理 3、还可以
自动将运行时异常,作为回调的参数(用于捕获异常,
提示) session过期,session=null,要
跳转登录页面。跳出iframe,分ajax请求的
跳转,普通连接的
跳转 package com.wondersgroup.employeeBenefits.core.author.util; import java.io.PrintWriter; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; import com.wondersgroup.employeeBenefits.core.bases.model.User; import com.wondersgroup.employeeBenefits.core.bases.util.SpringWebHolder; public class HandlerSessionInterceptor extends HandlerInterceptorAdapter {//此处一般继承HandlerInterceptorAdapter适配器即可 @Override public boolean preHandle(HttpServletRequest request,HttpServletResponse response,Object handler) throws Exception { request.setCharacterEncoding("UTF-8"); response.setCharacterEncoding("UTF-8"); response.setContentType("text/html;charset=UTF-8"); HttpSession session= SpringWebHolder.getSession(); User user = (User) session.getAttribute("LOGIN_USER"); String uri = request.getRequestURI(); uri.substring(0,uri.indexOf("/",1)); String[] notFilter = new String[] { "login","validateCode","index.do","msg.do","sendmail","mailcheck","sendSMS","assign","queryDictDataByParentId","eidtcode","eidtcodemail","saveusercode"}; if (user==null)//判断session里是否有
用户信息 { boolean doFilter = true; for (String s : notFilter) { if (uri.indexOf(s) != -1) { // 如果uri中包含不过滤的uri,则不进行过滤 doFilter = false; break; } } if(doFilter){ if (request.getHeader("x-requested-with") != null && request.getHeader("x-requested-with") .equalsIgnoreCase("XMLHttpRequest"))//如果是ajax请求响应头会有,x-requested-with; { response.setHeader("sessionstatus","timeout");//在响应头设置session状态 response.setHeader("url",uri.substring(0,1))); }else{ PrintWriter out = response.getWriter(); StringBuilder builder = new StringBuilder(); builder.append("<script type=\"text/javascript\" charset=\"UTF-8\">"); builder.append("alert(\"
页面过期,请重新
登录\");"); builder.append("window.top.location.href=\""); builder.append(uri.substring(0,1))+"/login.do"); builder.append("\";</script>"); out.print(builder.toString()); out.close(); } return false; // } // response.setHeader("sessionstatus","timeout");//在响应头设置session状态 // return false; } } return true; } @Override public void postHandle(HttpServletRequest request,Object handler,ModelAndView modelAndView) throws Exception { System.out.println("===========HandlerInterceptor1 postHandle"); } @Override public void afterCompletion(HttpServletRequest request,Exception ex) throws Exception { System.out.println("===========HandlerInterceptor1 afterCompletion"); } ========================== 在公用的js中 /** * 设置未来(全局)的AJAX请求默认选项 * 主要设置了AJAX请求遇到Session过期的情况 */ $.ajaxSetup({ contentType:"application/x-www-form-urlencoded;charset=utf-8",error: function (XMLHttpRequest,textStatus,errorThrown){ if(XMLHttpRequest.status==
403){ alert('您没有权限访问此资源或进行此操作'); return false; } },complete:function(XMLHttpRequest,textStatus){ var sessionstatus=XMLHttpRequest.getResponseHeader("sessionstatus"); //通过XMLHttpRequest取得响应头,sessionstatus, var url=XMLHttpRequest.getResponseHeader("url"); if(sessionstatus=='timeout'){ //如果超时就处理 ,指定要
跳转的
页面 var top = getTopWinow(); //
获取当前页面的顶层
窗口对象 alert('
登录超时,请重新
登录.'); top.location.href=url+"/login.do"; //
跳转到登陆
页面 } } }); /** * 在
页面中任何嵌套层次的窗口中
获取顶层窗口 * @return
当前页面的顶层
窗口对象 */ function getTopWinow(){ var p = window; while(p != p.parent){ p = p.parent; } return p; }
原文链接:https://www.f2er.com/ajax/162828.html