由于页面长时间未操作,出现登录超时,用户再次刷新页面,系统会自动跳转到登录页面。当用户重新登录后,怎么才能跳转到原来的页面呢?解决的方法如下:
/** * 获取request请求中的参数 * @param sb * @param req * @return */ @SuppressWarnings("all") private StringBuffer getRequestParameters(StringBuffer sb, HttpServletRequest req){ Map map = req.getParameterMap(); String str = "?"; if(!map.isEmpty()){ for(Object key : map.keySet()){ String[] values = (String[])map.get(key); for(String value:values){ str+=key+"="+value+"&"; } } str = str.substring(0, str.length()-1); } return sb.append(str); }
/** * 在调用Controller处理方法前,执行是否登录验证操作, * 如果没有登录直接跳转到登录页面 */ public boolean preHandle(HttpServletRequest req, HttpServletResponse resp, Object arg2) throws Exception { HttpSession session = req.getSession(); // 获取管理员账户信息 CatalogManager user = (CatalogManager)session.getAttribute("userinfo"); // 如果用户信息为null,则判断当前用户未登录,跳转到登录页面先登录 if(null == user){ //获取当前浏览器访问地址 StringBuffer urlBuff = req.getRequestURL(); getRequestParameters(urlBuff,req); //对要跳转的url经行编码 String path = req.getContextPath(); int port = req.getServerPort(); String portStr = 80 == port ? "" : (":"+req.getServerPort()); String basePath = req.getScheme()+"://"+req.getServerName()+portStr+path+"/"; //对要跳转的url经行编码 String url = URLEncoder.encode(urlBuff.toString(), HttpHelperCommon.CHARSET_ENCODING); resp.sendRedirect(basePath+"ht/login/toHtLogin.do?url="+url); return false; } else { //如果登录成功,则直接执行下一步操作 //ActionContext.getContext().getSession().put("userid", user.getId()); session.setAttribute("userinfo", user); session.setAttribute("userid", user.getId()); session.setAttribute("loginname", user.getUsername()); return true; } }