关于配置项目中应用ExtJs开发的拦截器,针对session过期后,点击任何操作都让页面跳转到登录界面。
编写好一个拦截器,先按常规配置好(那些配置拦截器的细节就不用说了吧,不会的请看下关于struts2的拦截器定义及其在struts.xml文件中的配置)。
先在拦截器里面判断session中是否存在’用户信息’,如果不存在则跳转到登录界面: public String intercept(ActionInvocation arg0) throws Exception {
} HttpSession session = if(session.getAttribute("user") == null){ http://www.wendangwang.com("用户没登录或登录过期,不能访问"); return "relogin"; ServletActionContext.getRequest().getSession(); }else{ http://www.wendangwang.com("用户已经登录,继续访问"); } return arg0.invoke();
下面就开始对Ext部分进行拦截了,因为Ext中提交的方法最终都是属于Ajax异步提交,普通拦截器不做特殊处理,所以就还得在上面的代码里面做处理,如下所示: public String intercept(ActionInvocation arg0) throws Exception {
HttpSession session = HttpServletResponse response = HttpServletRequest request = ServletActionContext.getRequest(); PrintWriter pw = response.getWriter(); if(session.getAttribute("user") == null){ String flag = ""; if(request.getHeader("X-Requested-With") != null && ServletActionContext.getRequest().getSession(); ServletActionContext.getResponse();
request.getHeader("X-Requested-With").equalsIgnoreCase("XMLHttpRequest")){
http://www.wendangwang.com("用户没登录或登录过期,不能访问"); response.setCharacterEncoding("text/html;charset=utf-8"); } http://www.wendangwang.com("用户已经登录,继续访问"); response.setContentType("text/html;charset=utf-8"); flag = "9999"; pw.write(flag); return null; http://www.wendangwang.com("用户没登录或登录过期,不能访问"); return "relogin"; }else{ }else{