如果按下浏览器后退按钮,如何防止在Spring MVC成功登录后返回登录表单?

时间:2017-09-28 20:19:08

标签: java spring-mvc back-button

控制器类:

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中,有一个“/ first”请求处理方法的链接,它会检查session的“visited”属性,如果它不为null,它将返回form.jsp,其中我们设置了“visited” “会话属性为true,否则返回home.jsp。 问题是,即使会话属性“visited”设置为true,我仍然可以通过单击浏览器中的后退按钮返回到form.jsp,但如果我输入URL栏http://localhost:9001/ExperimentProject1/first,我将被重定向到home.jsp,因为确实“visited”属性已设置为true。谁能解释一下这里发生了什么?为什么单击后退按钮对手动输入URI有不同的影响? 这只是登录机制的类比。

1 个答案:

答案 0 :(得分:0)

使用“redirect:home”而不是“home”。这可能是最着名的方法。