<!--
启动Spring MVC的注解功能,完成请求和注解POJO的映射,添加拦截器,类级别的处理器映射
-->
拦截器配置
public class HandlerInterceptor1 extends HandlerInterceptorAdapter {//此处一般继承HandlerInterceptorAdapter适配器即可
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
if (request.getHeader("x-requested-with") != null&& request.getHeader("x-requested-with").equalsIgnoreCase("XMLHttpRequest"))//如果是ajax请求响应头会有,x-requested-with; </span><div style="color: rgb(153, 51, 102);"><span style="font-size:12px;"> { </span></div><div style="color: rgb(153, 51, 102);"><span style="font-size:12px;"> response.setHeader("sessionstatus", "timeout");//在响应头设置session状态
response.getWriter().print("timeout"); //打印一个返回值,没这一行,在tabs页中无法跳出(<span style="font-size:12px;">导航栏能跳出</span>)
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
System.out.println("===========HandlerInterceptor1 postHandle");
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
System.out.println("===========HandlerInterceptor1 afterCompletion");
}
}
需要一个全局的js
$.ajaxSetup({
error: function (XMLHttpRequest, textStatus, errorThrown){
if(XMLHttpRequest.status==403){
alert('您没有权限访问此资源或进行此操作');
return false;
}
},
complete:function(XMLHttpRequest,textStatus){
var sessionstatus=XMLHttpRequest.getResponseHeader("sessionstatus"); //通过XMLHttpRequest取得响应头,sessionstatus,
if(sessionstatus=='timeout'){
//如果超时就处理 ,指定要跳转的页面
var top = getTopWinow(); //获取当前页面的顶层窗口对象
alert('登录超时, 请重新登录.');
top.location.href=path+"/login.jsp"; //跳转到登陆页面
}
}
});
/**
* 在页面中任何嵌套层次的窗口中获取顶层窗口
* @return 当前页面的顶层窗口对象
*/
function getTopWinow(){
var p = window;
while(p != p.parent){
p = p.parent;
}
return p;
}