后端控制器之CommondController、 FormController(二)

上一篇文章中存在一个问题,就是在表单提交后的成功视图中刷新网页时,刚刚提交的表单会再提交一次。这个问题被称作重复表单提交。为了避免这个问题,建议在成功地提交一个表单之后,要重定向到另一个URL,而不是直接返回一个HTML页面。

在web应用程序上下文中配置了ResourceBundleViewResolver,因此可以在classpath根部的views.properties中定义如下的重定向视图。

Xml代码
  1. #重定向视图,解决SimpleFormController提交表单后,重复提交表单  
  2. viewSuccessRedirect.(class) = org.springframework.web.servlet.view.RedirectView 
  3. viewSuccessRedirect.url =index.jsp 

将RegistrationController的成功视图指定为该重定向的视图。现在,当表单提交成功时,用户会被重定向到另一个URL,即使用户刷新了这个页面,也不会导致重复提交表单的问题。

Xml代码
  1. <?xml version="1.0" encoding="UTF-8" ?> 
  2. <beans xmlns="http://www.springframework.org/schema/beans" 
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  4.     xmlns:p="http://www.springframework.org/schema/p" 
  5.     xmlns:context="http://www.springframework.org/schema/context" 
  6.     xmlns:mvc="http://www.springframework.org/schema/mvc" 
  7.     xsi:schemaLocation="     
  8.            http://www.springframework.org/schema/beans     
  9.            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd     
  10.            http://www.springframework.org/schema/context     
  11.            http://www.springframework.org/schema/context/spring-context-3.0.xsd    
  12.            http://www.springframework.org/schema/mvc     
  13.            http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> 
  14.      
  15.     <!-- 初始化bean,指定初始页面和成功后的页面 --> 
  16.     <bean id="registrationController" 
  17.         class="com.wy.controller.RegistrationController"> 
  18.         <property name="commandClass" value="com.wy.pojo.User"/> 
  19.         <property name="formView"    value="login" /> 
  20.         <property name="successView" value="viewSuccessRedirect" /> 
  21.     </bean> 
  22.      
  23.     <!-- 映射处理器 --> 
  24.     <bean id="simpleUrlMapping" 
  25.         class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> 
  26.         <property name="mappings"> 
  27.             <props> 
  28.                 <prop key="/register.do">registrationController</prop> 
  29.             </props> 
  30.         </property> 
  31.     </bean> 
  32.      
  33.     <!-- 视图解析器 --> 
  34.     <bean id="viewResolver" 
  35.         class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
  36.         <property name="prefix" value="/WEB-INF/page/" /> 
  37.         <property name="suffix" value=".jsp" /> 
  38.         <property name="order" value="1" />   
  39.     </bean> 
  40.      
  41.     <bean id="resourceBundleViewResolver" 
  42.         class="org.springframework.web.servlet.view.ResourceBundleViewResolver"> 
  43.         <property name="basename" value="views" /> 
  44.         <property name="defaultParentView" value="views" /> 
  45.         <property name="order" value="0" />  
  46.     </bean>  
  47.      
  48.     <bean name="/parameterizableViewController.do" class="org.springframework.web.servlet.mvc.ParameterizableViewController">   
  49.         <property name="viewName" value="viewSuccessRedirect" />   
  50.     </bean> 
  51. </beans> 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值