一般使用Struts2的拦截器(或者是filter)验证是否登录的时候,如果用户没有登录则会跳转到登录的页面。这时候一般可以在拦截器或者filter中用response.sendRedirect()。
但当在页面上使用了iframe后,发现跳转的只是页面中iframe内的区域,而父页面却没有跳转。拦截器或者过滤器中发送重定向请求时,是在iframe页面发送的。
原来的代码是这样的:
因为response.sendRedirect()没有target属性,但html页面和js中有。于是,当判断出用户不具备访问权限时,可以在jsp中使用js来转向到真正的登录页面(这里我是采用的中间页面,但是我又不希望它显示出来,于是就要使用到JS了)。具体的代码可以这样写:
@Override public String intercept(ActionInvocation invocation) throws Exception { /* If the user have not been logined , it must return Login_Fail and forward to login.jsp */ if( ActionContext.getContext().getSession().get("login_user") == null ) { HttpServletResponse response = ServletActionContext.getResponse(); PrintWriter out = response.getWriter(); out.println("<html>"); out.println("<script type=/"text/javascript/">"); out.println("window.open ('login','_top')"); out.println("</script>"); out.println("</html>"); return null; } else { return invocation.invoke(); } }
OK!搞定!