首先看一下AbstractFormController中handleRequestInternal()的代码。
protected ModelAndView handleRequestInternal(HttpServletRequest request,<wbr style="LINE-HEIGHT: 1.3em">
HttpServletResponse response) throws Exception {</wbr><wbr style="LINE-HEIGHT: 1.3em">
// Form. submission or new form. to show?</wbr><wbr style="LINE-HEIGHT: 1.3em">
if (isFormSubmission(request)) {</wbr><wbr style="LINE-HEIGHT: 1.3em">
// Fetch form. object from HTTP session, bind, validate, process submission.</wbr><wbr style="LINE-HEIGHT: 1.3em">
try {</wbr><wbr style="LINE-HEIGHT: 1.3em">
Object command = getCommand(request);</wbr><wbr style="LINE-HEIGHT: 1.3em">
ServletRequestDataBinder binder = bindAndValidate(request, command);</wbr><wbr style="LINE-HEIGHT: 1.3em">
BindException errors = new BindException(binder.getBindingResult());</wbr><wbr style="LINE-HEIGHT: 1.3em">
return processFormSubmission(request, response, command, errors);</wbr><wbr style="LINE-HEIGHT: 1.3em">
}</wbr><wbr style="LINE-HEIGHT: 1.3em">
catch (HttpSessionRequiredException ex) {</wbr><wbr style="LINE-HEIGHT: 1.3em">
// Cannot submit a session form. if no form. object is in the session.</wbr><wbr style="LINE-HEIGHT: 1.3em">
if (logger.isDebugEnabled()) {</wbr><wbr style="LINE-HEIGHT: 1.3em">
logger.debug("Invalid submit detected: " + ex.getMessage());</wbr><wbr style="LINE-HEIGHT: 1.3em">
}</wbr><wbr style="LINE-HEIGHT: 1.3em">
return handleInvalidSubmit(request, response);</wbr><wbr style="LINE-HEIGHT: 1.3em">
}</wbr><wbr style="LINE-HEIGHT: 1.3em">
}</wbr><wbr style="LINE-HEIGHT: 1.3em">
else {</wbr><wbr style="LINE-HEIGHT: 1.3em">
// New form. to show: render form. view.</wbr><wbr style="LINE-HEIGHT: 1.3em">
return showNewForm(request, response);</wbr><wbr style="LINE-HEIGHT: 1.3em">
}</wbr><wbr style="LINE-HEIGHT: 1.3em">
}</wbr><wbr style="LINE-HEIGHT: 1.3em">
SimpleFormController继承自AbstractFormControoler,程序进入到handleRequestInternal(),首先判断是否是提交数据,显然不是的。所以调用showNewForm来显示一个表单,showNewForm调用了formBackingObject()来取得一个表单对象,默认是返回一个commandName指定的类。在修改操作时,可以重载该方法从[url=javascript:;]<wbr><wbr>数据库</wbr></wbr><wbr></wbr><wbr>[/url]中读取一个表单对象。接下来的initBinder不提。然后showNewForm调用了showForm(),showForm就是显示表单,它首先会调用referenceData(),referenceData默认是返回null。你可以重载它在其中填充一些需要显示在页面上的其他信息。可以返回一个map也可以直接在方法体内使用request.setAttribute()。 </wbr><wbr style="LINE-HEIGHT: 1.3em"></wbr></wbr><wbr style="LINE-HEIGHT: 1.3em">
</wbr><wbr style="LINE-HEIGHT: 1.3em">
程序员可以通过isFormSubmission()来判断用户是否执行提交按钮,如果执行提交,那么这个时候如果设置了isSessionForm(默认为false),程序将从session中查找绑定的对象来接受表单的值。一般情况下isSessionForm默认是false,所以程序会调用formBackingObject()来获取表单数据。<wbr style="LINE-HEIGHT: 1.3em"></wbr></wbr><wbr style="LINE-HEIGHT: 1.3em">
</wbr><wbr style="LINE-HEIGHT: 1.3em">
获取表单数据后,如果挂有validator,那么执行validator中的验证逻辑。验证通过后,进行用户指定的操作,跳转到成功页面。验证失败则跳转当前表单页面,使用showForm(request, response, errors),其中errors对象包含了commandName对象。所以页面可以保留我们的输入数据。<wbr style="LINE-HEIGHT: 1.3em"></wbr></wbr><wbr style="LINE-HEIGHT: 1.3em">
protected ModelAndView handleRequestInternal(HttpServletRequest request,<wbr style="LINE-HEIGHT: 1.3em">
HttpServletResponse response) throws Exception {</wbr><wbr style="LINE-HEIGHT: 1.3em">
// Form. submission or new form. to show?</wbr><wbr style="LINE-HEIGHT: 1.3em">
if (isFormSubmission(request)) {</wbr><wbr style="LINE-HEIGHT: 1.3em">
// Fetch form. object from HTTP session, bind, validate, process submission.</wbr><wbr style="LINE-HEIGHT: 1.3em">
try {</wbr><wbr style="LINE-HEIGHT: 1.3em">
Object command = getCommand(request);</wbr><wbr style="LINE-HEIGHT: 1.3em">
ServletRequestDataBinder binder = bindAndValidate(request, command);</wbr><wbr style="LINE-HEIGHT: 1.3em">
BindException errors = new BindException(binder.getBindingResult());</wbr><wbr style="LINE-HEIGHT: 1.3em">
return processFormSubmission(request, response, command, errors);</wbr><wbr style="LINE-HEIGHT: 1.3em">
}</wbr><wbr style="LINE-HEIGHT: 1.3em">
catch (HttpSessionRequiredException ex) {</wbr><wbr style="LINE-HEIGHT: 1.3em">
// Cannot submit a session form. if no form. object is in the session.</wbr><wbr style="LINE-HEIGHT: 1.3em">
if (logger.isDebugEnabled()) {</wbr><wbr style="LINE-HEIGHT: 1.3em">
logger.debug("Invalid submit detected: " + ex.getMessage());</wbr><wbr style="LINE-HEIGHT: 1.3em">
}</wbr><wbr style="LINE-HEIGHT: 1.3em">
return handleInvalidSubmit(request, response);</wbr><wbr style="LINE-HEIGHT: 1.3em">
}</wbr><wbr style="LINE-HEIGHT: 1.3em">
}</wbr><wbr style="LINE-HEIGHT: 1.3em">
else {</wbr><wbr style="LINE-HEIGHT: 1.3em">
// New form. to show: render form. view.</wbr><wbr style="LINE-HEIGHT: 1.3em">
return showNewForm(request, response);</wbr><wbr style="LINE-HEIGHT: 1.3em">
}</wbr><wbr style="LINE-HEIGHT: 1.3em">
}</wbr><wbr style="LINE-HEIGHT: 1.3em">
SimpleFormController继承自AbstractFormControoler,程序进入到handleRequestInternal(),首先判断是否是提交数据,显然不是的。所以调用showNewForm来显示一个表单,showNewForm调用了formBackingObject()来取得一个表单对象,默认是返回一个commandName指定的类。在修改操作时,可以重载该方法从[url=javascript:;]<wbr><wbr>数据库</wbr></wbr><wbr></wbr><wbr>[/url]中读取一个表单对象。接下来的initBinder不提。然后showNewForm调用了showForm(),showForm就是显示表单,它首先会调用referenceData(),referenceData默认是返回null。你可以重载它在其中填充一些需要显示在页面上的其他信息。可以返回一个map也可以直接在方法体内使用request.setAttribute()。 </wbr><wbr style="LINE-HEIGHT: 1.3em"></wbr></wbr><wbr style="LINE-HEIGHT: 1.3em">
</wbr><wbr style="LINE-HEIGHT: 1.3em">
程序员可以通过isFormSubmission()来判断用户是否执行提交按钮,如果执行提交,那么这个时候如果设置了isSessionForm(默认为false),程序将从session中查找绑定的对象来接受表单的值。一般情况下isSessionForm默认是false,所以程序会调用formBackingObject()来获取表单数据。<wbr style="LINE-HEIGHT: 1.3em"></wbr></wbr><wbr style="LINE-HEIGHT: 1.3em">
</wbr><wbr style="LINE-HEIGHT: 1.3em">
获取表单数据后,如果挂有validator,那么执行validator中的验证逻辑。验证通过后,进行用户指定的操作,跳转到成功页面。验证失败则跳转当前表单页面,使用showForm(request, response, errors),其中errors对象包含了commandName对象。所以页面可以保留我们的输入数据。<wbr style="LINE-HEIGHT: 1.3em"></wbr></wbr><wbr style="LINE-HEIGHT: 1.3em">