Spring MVC中 SimpleFormController流程解析

 首先看一下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"> b.gif
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值