一般情况下,我们使用“form”作为Spring MVC表单标签的前缀,当然只要愿意,你可以调整为其它的前缀名。在声明好标签引用后,就可以在该JSP文件中使用所有Spring MVC的表单标签了。下面是一个使用<form:form>表单标签的示例,它将最终生成一个HTML的 form表单:<% @ page language = " java " contentType = " text/html; charset=UTF-8 " pageEncoding = " UTF-8 " %> <% @ taglib prefix = " form " uri = " http://www.springframework.org/tags/form " %> ①引入标签的声明 < html > … ②声明后,在页面中就可以使用任意Spring表单标签了 </ html >
< form:form > 用户名: < form:input path ="userName" /> < br > 密 码: < form:password path ="password" />< br > Email: < form:input path ="email" />< br > < input type ="submit" value ="注册" name ="testSubmit" /> < input type ="reset" value ="重置" /> </ form:form >
目录
|
说明
|
cssClass | 使用该属性指定表单元素CSS样式名,相当于HTML元素的class属性。示例:<form:input path="userName" cssClass="inputStyle"/>。 |
cssStyle |
直接通过该属性指定样式,相当于HTML元素的style属性。示例:
<form:input path="userName" cssStyle="width:100px"/>。
|
cssErrorClass | cssClass表示表单元素未发生错误时对应的样式,而cssErrorClass表示表单元素发生错误时对应的样式,示例: |
< form:form > 用户名: < form:input path ="userName" /> < br > ①单行文件框标签 密 码: < form:password path ="password" />< br > ②密码框标签 描 述: < form:textarea path ="desc" cols ="20" rows ="3" />< br > ③多行文件框标签 < form:hidden path ="times" /> ④隐藏组件的值 < input type ="submit" value ="注册" name ="testSubmit" /> < input type ="reset" value ="重置" /> </ form:form >
正如你看到的,所有表单组件标签都通过path属性绑定表单对象的属性值,它支持级联属性,比如path="user.userName"将调用表单对象getUser.getUserName()绑定表单对象的属性值。这些表单组件标签拥有大多数HTML组件标签的镜像属性,如③处的<form:textarea>就使用了cols和rows属性设定列数和行数。
以上使用表单标签的页面的对应HTML页面如下所示:
< form id ="command" method ="post" action ="/baobaotao//registerUser.html" > 用户名: < input id ="userName" name ="userName" type ="text" value ="" />< br > 密 码: < input id ="password" name ="password" type ="password" value ="" />< br > 描 述: < textarea id ="desc" name ="desc" rows ="3" cols ="20" ></ textarea >< br > < input id ="times" name ="times" type ="hidden" value ="0" /> < input type ="submit" value ="注册" name ="testSubmit" /> < input type ="reset" value ="重置" /> </ form >
单选框和复选框组件标签
单选框和复选框组件虽然在HTML中都对应<input>元素标签,但在Spring MVC表单标签中,它们分别对应两个更达意的标签:
checkbox< form:radiobutton > 和 < form:checkbox > 。 radiobutton 单选框组件由两个同名的标签组件组成,当表单对象对应属性值和value值相等时,单选框选中。下面是一个代表性别的单选框: < form:form > 性 别: < form:radiobutton path ="sex" value ="0" /> 男 < form:radiobutton path ="sex" value ="1" /> 女 </ form:form > 当表单对象的sex属性为0时(可以是String、int等可以自动转换为String的类型),所生成的HTML代码如下所示: < form id ="command" method ="post" action ="/baobaotao//registerUser.html" > 性 别: < input id ="sex1" name ="sex" type ="radio" value ="0" checked ="checked" /> 男 < input id ="sex2" name ="sex" type ="radio" value ="1" /> 女 </ form >
package com.baobaotao.web.user; … import org.springframework.ui.ModelMap; public class UserRegisterController extends SimpleFormController { private BbtForum bbtForum; ①创建初始表单对象 protected Object formBackingObject(HttpServletRequest request) throws Exception { int userId = ServletRequestUtils.getIntParameter(request, " userId " , - 1 ); User user = bbtForum.getUser(userId); user.setUserName( " tom " ); List favorites = new ArrayList();① - 1默认选中值为1和3的选项 favorites.add( " 1 " ); favorites.add( " 3 " ); user.setFavorites(favorites); return user; } @Override ②准备表单显示时需要的数据 protected Map referenceData(HttpServletRequest request) throws Exception { Map favoriteMap = new LinkedHashMap(); favoriteMap.put( " 1 " , " computer " ); favoriteMap.put( " 2 " , " sport " ); favoriteMap.put( " 3 " , " entertainment " ); favoriteMap.put( " 4 " , " literature " ); ② - 1将表单页面需要的对象以ModelMap返回,最终将以属性名值对方式出现在请求属性中 return new ModelMap().addObject( " favoriteMap " , favoriteMap); } @Override protected ModelAndView onSubmit(Object command, BindException errors) throws Exception { User user = (User) command; bbtForum.registerUser(user); return new ModelAndView(getSuccessView(), " user " , user); } }