Handling duplicate form submission in Spring MVC

In last Spring MVC form handling example, if you refresh the form success view, most browsers will prompt a pop-up dialog to confirm about the form resubmission. If you click “yes”, the form will be resubmitted again, this scenario is well-known as duplicated form submission.

Figure : example of duplicated form submission.
SpringMVC-Duplicate-Form-Submit

The common solution to this is using “Post/Redirect/Get” Design Pattern. It will redirect to another URL if the form submission is successfully, instead of returning a web page directly.

Post/Redirect/Get Design Pattern in Spring MVC

In this tutorial, we show you how to apply the “Post/Redirect/Get” Design Pattern in Spring MVC to solve the duplicated form submission problem in last form handling example.

1. Duplicate form submission

See below normal form declaration that will hits the duplicate form submission problem.

File : mvc-dispatcher-servlet.xml
<bean 
class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" />

   <bean class="com.mkyong.customer.controller.CustomerController">
    <property name="formView" value="CustomerForm" />
    <property name="successView" value="CustomerSuccess" />
   </bean>

   <bean id="viewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
        <property name="prefix">
             <value>/WEB-INF/pages/</value>
        </property>
        <property name="suffix">
             <value>.jsp</value>
        </property>
    </bean>

In above snippet, the CustomerController returns a “CustomerSuccess” view directly, which should be replace with a redirect URL instead.

2. Redirect View

Declared a review view, named “customerSuccessRedirect” and return an URL “CustomerSuccess.htm“.

File : spring-views.xml
<beans ...>
   <!-- Redirect view --> 
   <bean id="customerSuccessRedirect" 
       class="org.springframework.web.servlet.view.RedirectView">
       <property name="url" value="CustomerSuccess.htm" />
    </bean>  
</beans>

3. Spring Configuration

Update the mvc-dispatcher-servlet.xml settings to link all Spring’s configuration together.

  • Update the “successView” to the new redirect view, named “customerSuccessRedirect“.
  • Declare a “XmlViewResolver” to load the redirect view.
  • Put a priority order for the “InternalResourceViewResolver” and “XmlViewResolver“, otherwise the “InternalResourceViewResolver” will always match and give your application no chance to call the “XmlViewResolver“.
  • Declare a “ParameterizableViewController” controller to match the redirect URL and return a view to user. Since the “ControllerClassNameHandlerMapping” won’t generated the mapping for any build-in Spring’s controller, so you have to define the explicit mapping in “SimpleUrlHandlerMapping“.
File : mvc-dispatcher-servlet.xml
<bean 
class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" />

   <bean class="com.mkyong.customer.controller.CustomerController">
    <property name="formView" value="CustomerForm" />
    <property name="successView" value="customerSuccessRedirect" />

    <!-- it was
    <property name="successView" value="CustomerSuccess" />
    -->
   </bean>

   <!-- Redirect Controller -->
   <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="mappings">
            <props>
                <prop key="/CustomerSuccess.htm">customerSuccessController</prop>
            </props>
        </property>
   </bean>

   <bean id="customerSuccessController" 
        class="org.springframework.web.servlet.mvc.ParameterizableViewController">
       <property name="viewName" value="CustomerSuccess" />
   </bean>

   <bean id="viewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
        <property name="prefix">
             <value>/WEB-INF/pages/</value>
        </property>
        <property name="suffix">
             <value>.jsp</value>
         </property>
         <property name="order" value="1" />
   </bean>

   <bean class="org.springframework.web.servlet.view.XmlViewResolver">
        <property name="location">
          <value>/WEB-INF/spring-views.xml</value>
     </property>
     <property name="order" value="0" />
   </bean>  

4. How it works?

  1. Access URL : http://localhost:8080/SpringMVC/customer.htm.

  2. Fill in and submits the form.

  3. Return “successView”, which is “customerSuccessRedirect“.

   <bean class="com.mkyong.customer.controller.CustomerController">
    <property name="formView" value="CustomerForm" />
    <property name="successView" value="customerSuccessRedirect" />
   </bean>
  1. “XmlViewResolver” match it and return a “RedirectView” with URL “CustomerSuccess.htm“.
   <bean id="customerSuccessRedirect" 
       class="org.springframework.web.servlet.view.RedirectView">
       <property name="url" value="CustomerSuccess.htm" />
    </bean>
  1. SimpleUrlHandlerMapping” match it and return a ParameterizableViewController, “customerSuccessController“, and return the view name “CustomerSuccess“.
   <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="mappings">
            <props>
                <prop key="/CustomerSuccess.htm">customerSuccessController</prop>
            </props>
        </property>
   </bean>
   <bean id="customerSuccessController" 
        class="org.springframework.web.servlet.mvc.ParameterizableViewController">
       <property name="viewName" value="CustomerSuccess" />
   </bean>

6. “InternalResourceViewResolver” match it and return the final view “/WEB-INF/pages/CustomerSuccess.jsp“.

   <bean id="viewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
        <property name="prefix">
             <value>/WEB-INF/pages/</value>
        </property>
        <property name="suffix">
             <value>.jsp</value>
         </property>
         <property name="order" value="1" />
   </bean>

7. URL changed to http://localhost:8080/SpringMVC/CustomerSuccess.htm.

8. Try refresh the success form page , the form resubmission dialog will not prompt anymore.

Note
The overall concept is return a redirect URL instead of a direct page.

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值