1. Spring提供的轻量级标签库

2.可在JSP页面中渲染HTML元素的标签

3 用法

  1)必须在JSP页面的开头处声明taglib指令

    <%@ taglib prefix="fm"  uri="http://www.springframework.org/tags/form" %>

  2)引入标签声明之后就可使用Spring表单标签

    <fm:form/>

    <fm:input/>

    <fm:password/>

    <fm:hidden/>

    <fm:textarea/>

    <fm:radiobutton/>

    <fm:checkbox/>

    <fm:select/>

    <fm:error/>

4.<fm:form>标签

  1)modelAttribute

    指定绑定的模型属性,默认为command

    建议指定

  2)action

    指定表单提交的目标URL

    可不指定,则自动提交到获取表单页面的URL

  3)method

    GET

    POST

        例:在进入form页面前绑定模型

         public String addUserShow(@ModelAttribute("user")User user){

        return "useradd";

      }

5. <fm:input/>标签

  path       属性路径,表示表单对象属性,如userName、userCode等

  cssClass   表单组件对应的CSS样式类名

  cssErrorClass  当提交表单后报错(服务端错误),采用的CSS样式类

  cssStyle   表单组件对应的CSS样式

  htmlEscape 绑定的表单属性值是否要对HTML特殊字符进行转换,默认为true

  注意: 表单组件标签也拥有HTML标签的各种属性,比如:id、onclick等等,都可以根据需要,灵活使用

// 跳转到添加页面add.jsp
	@RequestMapping("/toaddpage")
	public String toAddPage(Model model){
		// 查询出所有地址
		Collection<Address>  addresses = addressDao.getAddresss();
		// 添加到请求域中
		model.addAttribute("address", addresses);
		// 用于数据回显
		model.addAttribute("employee", new Employee("10", "卢俊义", 1, new Address("109","哈哈")));
		return "add";
	}
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fm"  uri="http://www.springframework.org/tags/form" %>

<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'hello.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
  </head>

<!-- 表单标签:
	通过SpringMVC的表单标签可以实现将模型数据中的属性和html表单元素相绑定,
	以实现表单数据更便捷编辑和表值的回显.
	1). SpringMVC认为,表单 数据中的每一项最终都是要回显的。
	    path指定的是一个属性,这个属性是从隐含模型(请求域中取出的某个对象中属性)
	    path指定的每一个属性,请求域中必须有一个对象(这个对象就是请求域中的command对应的对象),拥有这个属性;
	  
	  modelAttribute="",以前我们表单标签会从请求域中获取一个command对象;把这个对象中的每一个属性对应的显示出来.
	  modelAttribute="employee",可以告诉SpringMVC不要去取command的值了,我做了一个modelAttribute指定的值;取
	  对象时用的key就是我modelAttribute指定的"employee"
	            
 -->
 <fm:form action="emp" method="post" modelAttribute="employee">
    <!-- path就是原来html-input的name值;需要写
    	 path 1).当做原生的name项
    	      2).自动回显隐含模型中某个对象对应的这个属性的值
     -->
 	姓名: <fm:input path="name"/>   <br/>
 	性别: 男:<fm:radiobutton path="gender" value="1" /> &nbsp;&nbsp;&nbsp;&nbsp;
 		   女:<fm:radiobutton path="gender" value="0" /><br/>
    <!-- 
    	items="": 指定要遍历的集合;自动遍历;遍历出的每一个元素是一个address对象
    	itemLabel="属性名":指定遍历出的这个对象的哪个属性是作为option标签体的值
    	itemValue="属性名":指定刚才遍历出来的这个对象的哪个属性是作为要提交的value值
     -->
 	地址: <fm:select path="address.aid" items="${address}" itemLabel="aname" itemValue="aid">
 		  </fm:select>
 			<br/>   
 	<input type="submit" value="提交"/>	   
 </fm:form>

  <!--  
  <body>
    <form action="emp" method="post">
      	姓名: <input type="text" name="name"/>   <br/>
      	性别: 
      	     男:<input type="radio" name="gender" value="1"/> &nbsp;&nbsp;&nbsp;&nbsp;
      	     女:<input type="radio" name="gender" value="0"/> <br/>
      	地址:     
	    	<select name="address.aid">
	 			<c:forEach items="${address}" var="ads">
	 				<option value="${ads.aid}">${ads.aname}</option>
	 			</c:forEach>
	    	</select> <br/>
	    <input type="submit" value="提交"/>
    </form>
  </body>
  -->
</html>