上一篇文章中存在一个问题,就是在表单提交后的成功视图中刷新网页时,刚刚提交的表单会再提交一次。这个问题被称作重复表单提交。为了避免这个问题,建议在成功地提交一个表单之后,要重定向到另一个URL,而不是直接返回一个HTML页面。
在web应用程序上下文中配置了ResourceBundleViewResolver,因此可以在classpath根部的views.properties中定义如下的重定向视图。
#重定向视图,解决SimpleFormController提交表单后,重复提交表单 viewSuccessRedirect.(class) = org.springframework.web.servlet.view.RedirectView viewSuccessRedirect.url =index.jsp
将RegistrationController的成功视图指定为该重定向的视图。现在,当表单提交成功时,用户会被重定向到另一个URL,即使用户刷新了这个页面,也不会导致重复提交表单的问题。
<?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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<!-- 初始化bean,指定初始页面和成功后的页面 -->
<bean id="registrationController"
class="com.wy.controller.RegistrationController">
<property name="commandClass" value="com.wy.pojo.User"/>
<property name="formView" value="login" />
<property name="successView" value="viewSuccessRedirect" />
</bean>
<!-- 映射处理器 -->
<bean id="simpleUrlMapping"
class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/register.do">registrationController</prop>
</props>
</property>
</bean>
<!-- 视图解析器 -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/page/" />
<property name="suffix" value=".jsp" />
<property name="order" value="1" />
</bean>
<bean id="resourceBundleViewResolver"
class="org.springframework.web.servlet.view.ResourceBundleViewResolver">
<property name="basename" value="views" />
<property name="defaultParentView" value="views" />
<property name="order" value="0" />
</bean>
<bean name="/parameterizableViewController.do" class="org.springframework.web.servlet.mvc.ParameterizableViewController">
<property name="viewName" value="viewSuccessRedirect" />
</bean>
</beans>

被折叠的 条评论
为什么被折叠?



