---
title: "SimpleFormController简单表单控制器的处理流程"
author: "prodream"
date: "2008-08-26T10:19:00Z"
tags: []
categories: ["企业架构"]
slug: "simpleformcontroller"
draft: false
lastmod: "2010-01-30T17:40:17Z"
id: 578016
url: "https://prodream.iteye.com/blog/578016"
---

1．当表单控制器接收到GET请求时，它调用formBackingObject()方法，创建表单对象。该方法可以被子类覆盖，对于编辑操作的表单来说，你可以通过该方法从数据库中加载表单对象，当表单页面显示时，表单显示出待编辑的数据了；

2．表单对象和页面表单数据之间需要通过属性编辑器实现双向转化，对于非基本数据类型或String类型的属性来说，你可能需要注册一些自定义编辑器。你可以通过覆盖initBinder()方法，通过调用binder.registerCustomEditor()的方法注册编辑器；

3．表单对象通过bindOnNewForm属性（可以通过配置设置，默认为false）判断是否需要将GET请求参数绑定到formBackingObject()方法创建的表单对象中。如果bindOnNewForm为true，执行绑定操作，在绑定完成后，还将调用onBindOnNewForm()回调方法（子类可以提供具体实现）。否则到下一步。不过一般情况下，GET请求参数是用于加载等编辑表单对象的ID值，如topicId、forumId等，一般无需进行绑定；

4．调用referenceData()方法（子类可提供具体实现）准备一些关联的数据，如性别下拉框数据，学历下拉框数据等。一般采用ModelMap创建视图业务中需要用到的请求属性数据，键为属性名，值为属性值，如ModelMap("param1", "paramValue1")；

5．使用控制器formView定义的视图渲染表单对象；

6．用户填写或更改表单后，提交表单，向表单控制器发起一个POST请求；

7．接收到POST请求时，表单控制器知道这是一个表单数据提交的操作，所以启动表单提交处理流程；

8．首先通过sessionForm属性判断表单控制器是否启用了Session。如果启用了Session，直接从Session中取出原表单对象，否则再次调用formBackingObject()方法构造出一个表单对象。sessionForm默认为false，可以通过配置进行调整，启用Session可能提高运行性能，但会占用一定的内存；

9．将POST请求参数填充到表单对象中；

10．调用onBind()方法，该方法允许你在表单填充完成后，合法性校验之前执行一些特定的操作；

11．如果validateOnBinding属性设置为true，注册在控制器中的校验器开始工作，对表单对象的属性值执行合法性校验。如果有合法性错误，将被注册到Errors对象中（关于如何注册校验器，我们将稍后介绍）；

12．调用onBindAndValidate()方法，该方法允许你在数据绑定及合法性校验后，执行一些额外的自定义操作，你也可以在这里，执行一些额外的合法性校验；

13．调用processFormSubmission()方法处理提交任务，该方法内部又包含后续几步工作；

14．判断方法入参传入errors是否包含错误，如果包含错误返回到formView对应的表单页面中，否则到下一步；

15．通过isFormChangeRequest()方法（默认为false）判断请求是否为表单更改请求，如果为true，调用onFormChange()方法，然后返回到formView对应的表单页面，否则到下一步；

16．如果子类覆盖了onSubmit()方法，执行之，否则执行子类的doSubmitAction()方法。通过这两者之一完成业务的处理，然后返回successView属性指定的成功页面。

 

 

 

spring2.0 文档

　  
org.springframework.web.servlet.mvc.SimpleFormController 是AbstractFormController的子类别，对于简单的表单可以直接继承这个类别，并重新定义其onSubmit()或 doSubmitAction()方法以处理使用者的请求，SimpleFormController的处理流程是这样的：

1. 呼叫processFormSubmission()以检验 Errors 物件，看看在绑定或验证时有无任何的错误。
2. 如果有错误发生，返回"formView"所设定的页面。
3. 如果isFormChangeRequest()根据request被重新定义并返回true，则也会回到"formView"，在回到"formView"之前会呼叫 onFormChange() ，让您有机会修正表单物件。
4. 如果没有错误发生，则会呼叫带有HttpServletRequest、 HttpServletResponse、Object、BindException参数的onSubmit()，预设是呼叫仅带Object及 BindException的onSubmit()，而后者预设是呼叫只有Object参数的onSubmit()，预设是导向设定的 "successView"，可以考虑重新定义doSubmitAction()，不用返回ModelAndView，预设会导向 "successView"，在不需要向"successView"传送任何Model资料物件时可以使用。

 

**Method Summary** `protected void` `doSubmitAction(Objectcommand)`  
Template method for submit actions. `String` `getFormView()`  
Return the name of the view that should be used for form display. `String` `getSuccessView()`  
Return the name of the view that should be shown on successful submit. `protected boolean` `isFormChangeRequest(HttpServletRequestrequest)`  
Simpler `isFormChangeRequest` variant, called by the full variant [`isFormChangeRequest(HttpServletRequest, Object)`](http://doc.javanb.com/spring-framework-api-2-0-5/org/springframework/web/servlet/mvc/SimpleFormController.html#isFormChangeRequest%28javax.servlet.http.HttpServletRequest,%20java.lang.Object%29). `protected boolean` `isFormChangeRequest(HttpServletRequestrequest, Objectcommand)`  
Determine whether the given request is a form change request. `protected void` `onFormChange(HttpServletRequestrequest, HttpServletResponseresponse, Objectcommand)`  
Simpler `onFormChange` variant, called by the full variant [`onFormChange(HttpServletRequest, HttpServletResponse, Object, BindException)`](http://doc.javanb.com/spring-framework-api-2-0-5/org/springframework/web/servlet/mvc/SimpleFormController.html#onFormChange%28javax.servlet.http.HttpServletRequest,%20javax.servlet.http.HttpServletResponse,%20java.lang.Object,%20org.springframework.validation.BindException%29). `protected void` `onFormChange(HttpServletRequestrequest, HttpServletResponseresponse, Objectcommand, BindExceptionerrors)`  
Called during form submission if [`isFormChangeRequest(javax.servlet.http.HttpServletRequest)`](http://doc.javanb.com/spring-framework-api-2-0-5/org/springframework/web/servlet/mvc/SimpleFormController.html#isFormChangeRequest%28javax.servlet.http.HttpServletRequest%29) returns `true`. `protected ModelAndView` `onSubmit(HttpServletRequestrequest, HttpServletResponseresponse, Objectcommand, BindExceptionerrors)`  
Submit callback with all parameters. `protected ModelAndView` `onSubmit(Objectcommand)`  
Simplest `onSubmit` variant. `protected ModelAndView` `onSubmit(Objectcommand, BindExceptionerrors)`  
Simpler `onSubmit` variant. `protected ModelAndView` `processFormSubmission(HttpServletRequestrequest, HttpServletResponseresponse, Objectcommand, BindExceptionerrors)`  
This implementation calls [`showForm(HttpServletRequest, HttpServletResponse, BindException)`](http://doc.javanb.com/spring-framework-api-2-0-5/org/springframework/web/servlet/mvc/SimpleFormController.html#showForm%28javax.servlet.http.HttpServletRequest,%20javax.servlet.http.HttpServletResponse,%20org.springframework.validation.BindException%29) in case of errors, and delegates to the full [`onSubmit(HttpServletRequest, HttpServletResponse, Object, BindException)`](http://doc.javanb.com/spring-framework-api-2-0-5/org/springframework/web/servlet/mvc/SimpleFormController.html#onSubmit%28javax.servlet.http.HttpServletRequest,%20javax.servlet.http.HttpServletResponse,%20java.lang.Object,%20org.springframework.validation.BindException%29)'s variant else. `protected Map` `referenceData(HttpServletRequestrequest)`  
Create a reference data map for the given request. `protected Map` `referenceData(HttpServletRequestrequest, Objectcommand, Errorserrors)`  
Create a reference data map for the given request and command, consisting of bean name/bean instance pairs as expected by ModelAndView. `void` `setFormView(StringformView)`  
Set the name of the view that should be used for form display. `void` `setSuccessView(StringsuccessView)`  
Set the name of the view that should be shown on successful submit. `protected ModelAndView` `showForm(HttpServletRequestrequest, HttpServletResponseresponse, BindExceptionerrors)`  
This implementation shows the configured form view, delegating to the analogous [`showForm(HttpServletRequest, HttpServletResponse, BindException, Map)`](http://doc.javanb.com/spring-framework-api-2-0-5/org/springframework/web/servlet/mvc/SimpleFormController.html#showForm%28javax.servlet.http.HttpServletRequest,%20javax.servlet.http.HttpServletResponse,%20org.springframework.validation.BindException,%20java.util.Map%29) variant with a "controlModel" argument. `protected ModelAndView` `showForm(HttpServletRequestrequest, HttpServletResponseresponse, BindExceptionerrors, MapcontrolModel)`  
This implementation shows the configured form view. `protected boolean` `suppressValidation(HttpServletRequestrequest, Objectcommand)`  
This implementation delegates to [`isFormChangeRequest(HttpServletRequest, Object)`](http://doc.javanb.com/spring-framework-api-2-0-5/org/springframework/web/servlet/mvc/SimpleFormController.html#isFormChangeRequest%28javax.servlet.http.HttpServletRequest,%20java.lang.Object%29): A form change request changes the appearance of the form and should not get validated but just show the new form.