Simple Form Handling Example
继续 Spring MVC example anatomy 中springapp的例子。
PriceIncreaseController extends SimpleFormController。SimpleFormController 有2个视图:formView 用来显示 form,successView 则是 form 提交后转到的页面。
当我们从 hello.htm (实际是 hello.jsp) 上点击 priceincrease.htm 的超链接时,实际是发送了一个 GET 方法的请求,然后处理流程如下:

如果 priceincrease.jsp 页面上需要显示一些数据,可以通过 formBackingObject() 方法来初始化 Command Object,这个接下来再讲。
一般使用 DI 来设置 SimpleFormController 的 formView 和 successView:
<bean name="/priceincrease.htm" class="springapp.web.PriceIncreaseFormController">
<property name="sessionForm" value="true" />
<property name="commandName" value="priceIncrease" />
<property name="commandClass" value="springapp.service.PriceIncrease" />
<property name="validator">
<bean class="springapp.service.PriceIncreaseValidator" />
</property>
<property name="formView" value="priceincrease" />
<property name="successView" value="hello.htm" />
<property name="productManager" ref="productManager" />
</bean>
这里还有个有意思的事情:即使不配置 formView,根据 惯例优先原则,DispatchServlet 还是能找到 “priceincrease” 这个 viewName,然后去交给 InternalResourceViewResolver 解析 (如果配置了,应该是 SimpleFormController 执行 showForm 操作,返回一个 new ModelAndView (formView) 给 DispatchServlet)
我们注意到这里有 commandClass 和 commandName,这个还是相当于 jsp 页面的 Attribute。整个的提交的处理流程如下:

这里需要注意的有以下几点:
- Command Object 不需要名字。
commandName并不是 Command Object 的名字,所以在formBackingObject()方法里面创建 Command Object 不需要特别注意名字。formBackingObject()是一个覆写方法,父类中的方法应该也只是简单地 new 一个 Command Object - Command Object 的属性、JSP 中
<form>标签中的path、和Validator中的reject方法的参数存在对应关系,如下图所示 (文字描述太复杂了,画图算了):

<form:input path="percentage">表示把这个文本框的输入内容写入 Command Object (已通过commandName=“priceIncrease”指定);而<form:error path="percentage">会被展开为成一个<span>来显示错误信息 (if any),详见 The errors tag
留下评论