Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/387.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如果按下浏览器返回按钮,如何防止在SpringMVC中成功登录后返回登录表单?_Java_Spring Mvc_Back Button - Fatal编程技术网

Java 如果按下浏览器返回按钮,如何防止在SpringMVC中成功登录后返回登录表单?

Java 如果按下浏览器返回按钮,如何防止在SpringMVC中成功登录后返回登录表单?,java,spring-mvc,back-button,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;

控制器类:

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>

@导入url(“”);
登录表单

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>

拦截器
org.springframework.web.servlet.DispatcherServlet
上下文配置位置
/WEB-INF/config/springmvc-config.xml
1.
拦截器
/
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.jsp中,有一个指向“/first”请求处理方法的链接,该方法将检查会话的“visted”属性,如果该属性不为null,它将返回form.jsp,其中我们将会话的“visted”属性设置为true,否则返回home.jsp。 问题是,即使会话属性“visited”设置为true,我仍然可以通过单击浏览器中的后退按钮返回form.jsp,但是如果我键入URL栏,我将重定向到home.jsp,因为“visited”属性确实已设置为true。有人能解释一下这里发生了什么事吗?为什么单击后退按钮与手动键入URI有不同的效果?
这只是登录机制的一个类比。

使用“重定向:主页”而不是“主页”。这可能是最广为人知的方法。

使用“重定向:主页”而不是“主页”。这可能是最广为人知的方法。

更改登录页面,使其不执行正常的
POST,而是使用ajax执行,成功后将使用预期的目标页面替换当前页面。更改登录页面,使其不执行正常的
POST,而是使用ajax执行,成功后,用预期的目标页面替换当前页面。我已经尝试过了,但不起作用。在first()和second()方法中,如果我在home.jsp中点击back按钮,我仍然会进入form.jsp?我已经尝试过了,但没有成功。如果在first()和second()方法中都从home.jsp点击back按钮,我仍然会进入form.jsp?