SpringMVC学习笔记(二) --- 后端控制器之CommondController、 FormController(二)

上一篇文章中存在一个问题,就是在表单提交后的成功视图中刷新网页时,刚刚提交的表单会再提交一次。这个问题被称作重复表单提交。为了避免这个问题,建议在成功地提交一个表单之后,要重定向到另一个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>

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值