Controller類:如果在瀏覽器後退按鈕被按下的情況下,如何防止在Spring MVC中成功登錄後返回登錄表單?
package com.ym.controller;
import javax.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
public class CustomerController {
@RequestMapping(value="/home")
public String goHome(){
return "home";
}
@RequestMapping(value="/first")
public String first(HttpSession session){
if(session.getAttribute("visited") != null){
return "home";
}
else{
return "form";
}
}
@RequestMapping(value="/second", method=RequestMethod.POST)
public String second(HttpSession session){
session.setAttribute("visited", true);
return "home";
}
}
form.jsp
<%@ 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 uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<style type="text/css">@import url("<c:url value="/css/main.css"/>");
</style>
<title>Login Form</title>
</head>
<body>
<form:form action="second" method="post">
<p>
<input id="reset" type="reset" tabindex="4">
<input id="submit" type="submit" tabindex="5">
</p>
</form:form>
</body>
</html>
針對home.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!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">
<title>Home</title>
</head>
<body>
<a href="<c:url value="/first"/>">Go to form page</a>
</body>
</html>
的web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/config/springmvc-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
用SpringMVC-config.xml中
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.ym.controller"/>
<context:component-scan base-package="com.ym.service"/>
<mvc:annotation-driven/>
<mvc:resources mapping="/css/**" location="/css/"/>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
所以流量是,首先我們去「/ home」。在home.jsp中,有一個指向「/第一個」請求處理方法的鏈接,它將檢查session的「visited」屬性,如果它不爲null,它將返回form.jsp,在其中我們設置「visited 「會話屬性爲true,否則返回home.jsp。 問題是,即使會話屬性「visited」已設置爲true,我仍然可以通過單擊瀏覽器中的後退按鈕返回form.jsp,但如果我在URL欄中輸入http://localhost:9001/ExperimentProject1/first,我將重定向到home.jsp,因爲確實「visited」屬性已被設置爲true。有人能解釋我發生了什麼嗎?爲什麼點擊後退按鈕對手動輸入URI有不同的影響? 這只是登錄機制的一個類比。
更改登錄頁面,使其不會執行普通的'