解决session失效之后登陆后重新返回之前的页面

 

 

在全局拦截器设置保存之前的url存入session中 登陆之后的地址再重session中存

 request只用作一次请求 如果页面跳转几次的话原来的url就不存在了建议存在session

@Override
public boolean preHandle(HttpServletRequest request,
HttpServletResponse response, Object handler) throws Exception {
System.out.println("拦截Spring MVC所有的请求");
User user =(User) request.getSession().getAttribute("user");
if(user==null){
//用户没有登录

String url = request.getRequestURI();
//先清除之前url的session
try {
request.getSession().removeAttribute("url");
} catch (Exception e) {
// TODO: handle exception
}
//保留失效之前的地址
request.getSession().setAttribute("url", url);
response.sendRedirect(request.getContextPath()+"/failure.jsp");
return false;
}
return true;
}

posted on 2016-05-11 16:17  找到属于自己的天空  阅读(1256)  评论(0编辑  收藏  举报

导航