iframe中session超时,跳转打开窗口还是嵌入在iframe中问题解决

时间:2022-09-03 00:00:38

当我们在前台页面使用iframe框架时,在该框架中跳转链接时,时常跳转的链接还是在iframe框架中,

但是我们可能需要该链接直接跳出iframe框架到达我们想要显示的页面。

一、在jsp页面解决方案

window.open("<%=basePath%>main/logout","_parent");

二、在拦截器或者过滤器中解决方案

以下将以spring mvc中的拦截器为例进行演示

1、若是下面的代码,那将有下面图示的结果。

package com.sqc.base.interceptor;

import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

/**
* 登录拦截器
* 拦截器:只会拦截请求的action,需要在配置文件中配置
* @author Administrator
*
*/

public class LoginInterceptor implements HandlerInterceptor {

@Override
public void afterCompletion(HttpServletRequest request,
HttpServletResponse response, Object obj, Exception ex)
throws Exception {
// TODO Auto-generated method stub

}

@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response,
Object obj, ModelAndView model) throws Exception {
// TODO Auto-generated method stub

}

@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response,Object obj) throws Exception {
Object object=request.getSession().getAttribute("user");
if(object!=null){
return true;//继续向下执行
}

<span style="color:#ff0000;">String path=request.getContextPath();
response.sendRedirect(request.getContextPath()+"/login/toLogin");</span>
return false;//停止向下执行
}

}
以上的代码在session超时时,将有以下的图示,可以看见,我们的登录界面嵌入到了iframe中,但是这却不是为我们想要的结果

iframe中session超时,跳转打开窗口还是嵌入在iframe中问题解决

2、为了解决以上的问题,我们可以将拦截器中的代码变为如下所示:

package com.sqc.base.interceptor;

import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

/**
* 登录拦截器
* 拦截器:只会拦截请求的action,需要在配置文件中配置
* @author Administrator
*
*/

public class LoginInterceptor implements HandlerInterceptor {

@Override
public void afterCompletion(HttpServletRequest request,
HttpServletResponse response, Object obj, Exception ex)
throws Exception {
// TODO Auto-generated method stub

}

@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response,
Object obj, ModelAndView model) throws Exception {
// TODO Auto-generated method stub

}

@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response,Object obj) throws Exception {
Object object=request.getSession().getAttribute("user");
if(object!=null){
return true;//继续向下执行
}
<span style="color:#ff0000;">String url=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath()+"/login/toLogin";
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<script>");
out.println("window.open ('"+url+"','_parent')"); //作为父窗口打开
out.println("</script>");
out.println("</html>"); </span>

return false;//停止向下执行
}

}
以上的代码在session超时时,将直接跳出iframe框架,到达我们的登录界面

iframe中session超时,跳转打开窗口还是嵌入在iframe中问题解决

三、被嵌套的jsp页面中解决方案

<strong><script type="text/javascript">
$(function(){
</strong><span style="color:#ff0000;">if(window.parent!=window){//window.parent:如果不存在父窗口,那么该值默认为当前窗口对象
window.parent.location.reload(true);
}</span><strong>

})
</script></strong>