从该页面打开弹出窗口时,会话超时在主页面上不起作用

时间:2018-01-19 12:35:05

标签: javascript spring spring-mvc session popup

我正在开发一个小型弹簧MVC应用程序。当用户成功登录时,将显示欢迎页面,当会话超时时,它将自动重定向到登录页面。 在欢迎页面上,我有一个按钮,按下按钮会打开一个弹出窗口。现在打开弹出窗口后,如果会话超时,我的弹出窗口应该关闭,父页面应该重定向到登录页面。 我在父页面和弹出窗口中尝试了类似下面的元标记。

<meta http-equiv="refresh" content="<%=session.getMaxInactiveInterval()%>;url=login"/>

但它不起作用。当弹出窗口打开时,会话根本没有超时。页面是自动刷新的,但会话也没有在父页面中过期。

欢迎页面:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>  
<%@ page session="true" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<meta http-equiv="refresh" content="<%=session.getMaxInactiveInterval()%>;url=login"/>

<title>Home page</title>
<script>
    function popup() {
        window.open("../Bank/register", 'window', 'width=200,height=100');
    }
</script> 
</head>
<body>
<h1>Welcome ${sessionScope.USER_NAME}..!</h1>
    <c:if test="${sessionScope.USER_ROLE==1}">
        <!-- <a href="register">Register</a> -->
    <a href="#" onclick="popup()">Register</a>
    </c:if>
    <a href="logout">Logout</a>
</body>
</html>

弹出页面:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<meta http-equiv="refresh" content="<%=session.getMaxInactiveInterval()%>;url=login"/>
<title>Bank - Registration</title>
</head>
<body>
    <form:form action="register" method="post" modelAttribute="register">
        <table>
            <tr>
                <td><form:label path="username">User Name:</form:label></td>
                <td><form:input path="username" name="username" id="username" /></td>
            </tr>
            <tr>
                <td><form:label path="fName">First Name:</form:label></td>
                <td><form:input path="fName" name="fName" id="fName" /></td>
            </tr>
            <tr>
                <td><form:label path="lName">Last Name:</form:label></td>
                <td><form:input path="lName" name="lName" id="lName" /></td>
            </tr>
            <tr>
                <td><form:label path="password">Password:</form:label></td>
                <td><form:password path="password" name="password" id="password"/></td>
            </tr>
            <tr>
                <td><form:label path="Email">Email:</form:label></td>
                <td><form:input path="Email" name="Email" id="Email" /></td>
            </tr>
            <tr>
                <td><form:label path="phoneNo">Phone No:</form:label></td>
                <td><form:input path="phoneNo" id="phoneNo" name="phoneNo"/></td>
            </tr>
            <tr>
                <td><form:label path="IsAdmin">Is Admin:</form:label></td>
                <td><form:checkbox path="IsAdmin"  id="IsAdmin" name="IsAdmin" value="1"/></td>
            </tr>
            <tr>
                <td><form:button id="reset" name="reset" onClick="document.forms[0].reset();">Reset</form:button></td>
                <td><form:button id="register" name="register">Register</form:button></td>
            </tr>
        </table>

    </form:form>
    <table align="center">
        <tr>
            <td style="font-style: italic; color: red;">${Message}</td>
        </tr>
    </table>
</body>
</html>

如果我没有使用弹出窗口并且在单独的选项卡中仅使用URL重定向,那么它工作正常。但我不得不使用弹出窗口。请帮帮我。

1 个答案:

答案 0 :(得分:0)

通常,通过web.xml

处理Web应用程序中的会话超时

您可以在web.xml中包含以下代码段,它将处理超时。

<session-config>
  <session-timeout>30</session-timeout><!-- in minutes -->
</session-config>

如果您的应用程序中没有web.xml,则可以在应用程序容器session-timeout

中设置相同的web.xml

例如,如果您的应用程序容器是tomcat,则可以在$CATALINA_BASE/conf/web.xml找到web.xml,并且可以在该文件中定义session-timeout。在tomcat的情况下,默认值为30 min

无需在所有jsp文件中包含元标记<meta http-equiv="refresh" content="<%=session.getMaxInactiveInterval()%>;url=login"/>