Extjs与struts2整合,ajax请求登录验证

来源:互联网 发布:麻将手游源码 编辑:程序博客网 时间:2024/05/18 19:45

拦截器端,如下代码。

if (user == null) {HttpServletRequest request = ServletActionContext.getRequest();HttpServletResponse response = ServletActionContext.getResponse();PrintWriter pw = response.getWriter();String flag = "";if (request.getHeader("X-Requested-With") != null&& request.getHeader("X-Requested-With").equalsIgnoreCase("XMLHttpRequest")) {log.info("用户没登录或登录过期,不能访问");response.setCharacterEncoding("text/html;charset=utf-8");response.setContentType("text/html;charset=utf-8");flag = "9999";pw.write(flag);return null;} else {response.setCharacterEncoding("text/html;charset=utf-8");response.sendRedirect("/businessTest/login.jsp");log.info("用户没登录或登录过期,不能访问");return "login";}}

前台jsp端,进行页面跳转。

Ext.Ajax.on('requestcomplete',checkUserSessionStatus, this);   function checkUserSessionStatus(conn,response,options){var str = response.responseText;if(str == '9999'){alert('连接已超时,请重新登录!');window.location.href = 'login.jsp';}}

ok,未登录的话,发送ajax请求会被打回登录界面。


原创粉丝点击