JS设置防止页面回退
登录页面
<%--
Created by IntelliJ IDEA.
User: ASUS
Date: 2020/8/27
Time: 14:42
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>公寓管理系统 - 登录页面</title>
<link rel="stylesheet" href="css/index.css">
</head>
<body>
<div class="login">
<img src="image/logo.png" alt="智游">
<p>公寓管理系统</p>
<form action="<c:url value = "/login.action"/>" method="post">
<div>
<input type="text" id="username" name="wname" value="" placeholder="请输入用户名">
</div>
<div>
<input type="password" id="password" name="wpwd" value="" placeholder="请输入密码">
</div>
<c:if test="${not empty login_message}">
<p class="error-message">${login_message}</p>
</c:if>
<div><input type="submit" value="登录" class="input_submit"></div>
</form>
</div>
</body>
<script>
if (window.top !== window) {
window.top.location.reload();
}
</script>
</html>
在script标签中加上
history.pushState(null, null, document.URL);
window.addEventListener('popstate', function () {
history.pushState(null, null, document.URL);
});
如下图所示:
<script>
if (window.top !== window) {
window.top.location.reload();
}
history.pushState(null, null, document.URL);
window.addEventListener('popstate', function () {
history.pushState(null, null, document.URL);
});
</script>
将上方代码写到head和body的中间,因为它是从上到下依次执行,在下方的话,它运行需要时间。
<%--
Created by IntelliJ IDEA.
User: ASUS
Date: 2020/8/27
Time: 14:42
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>公寓管理系统 - 登录页面</title>
<link rel="stylesheet" href="css/index.css">
</head>
<script>
if (window.top !== window) {
window.top.location.reload();
}
history.pushState(null, null, document.URL);
window.addEventListener('popstate', function () {
history.pushState(null, null, document.URL);
});
</script>
<body>
<div class="login">
<img src="image/logo.png" alt="智游">
<p>公寓管理系统</p>
<form action="<c:url value = "/login.action"/>" method="post">
<div>
<input type="text" id="username" name="wname" value="" placeholder="请输入用户名">
</div>
<div>
<input type="password" id="password" name="wpwd" value="" placeholder="请输入密码">
</div>
<c:if test="${not empty login_message}">
<p class="error-message">${login_message}</p>
</c:if>
<div><input type="submit" value="登录" class="input_submit"></div>
</form>
</div>
</body>
</html>