Spring form 标签库与Spring Web MVC集成在一起,预设前提,表单支持对象的变量名是"command",最佳实践,可以将表单支持对象用其他名称加以定义,通过<form:form>标签中的commandName属性绑定领域对象。
如果<form:form>中没有指定method,默认是POST
Spring form标签库的配置:
1、在项目中include spring-webmvc.jar
2、在JSP中引入:<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<form:form>标签中比较有意思的几个标签是:
1、<form:checkboxs>,可以在运行期指定复选框列表。<form:checkboxes path="preferences.interests" items="${interestList}"/>,model 属性interestList可以是一个map,也可以是一个自定义对象的List或者Array,如果是map,则map的key作为值,value作为复选框的显示Label,如果是自定义对象,对象有两个属性,itemValue和itemLabel
2、<form:radiobuttons path="sex" items="${sexOptions}"/>,单选列表,用法与复选框列表相似
3、<form:errors path="firstName" />,提供在controller中和validation中创建的errors。
• path="*" - displays all errors
• path="lastName" - displays all errors associated with the lastName field