springmvc十九:springmvc表单标签
1. Spring提供的輕量級標(biāo)簽庫
2.可在JSP頁面中渲染HTML元素的標(biāo)簽
3 用法
1)必須在JSP頁面的開頭處聲明taglib指令
<%@ taglib prefix="fm" ?uri="http://www.springframework.org/tags/form" %>
2)引入標(biāo)簽聲明之后就可使用Spring表單標(biāo)簽
<fm:form/>
<fm:input/>
<fm:password/>
<fm:hidden/>
<fm:textarea/>
<fm:radiobutton/>
<fm:checkbox/>
<fm:select/>
<fm:error/>
4.<fm:form>標(biāo)簽
1)modelAttribute
指定綁定的模型屬性,默認(rèn)為command
建議指定
2)action
指定表單提交的目標(biāo)URL
可不指定,則自動提交到獲取表單頁面的URL
3)method
GET
POST
?? ??例:在進(jìn)入form頁面前綁定模型
???? public String addUserShow(@ModelAttribute("user")User user){
return "useradd";
}
5. <fm:input/>標(biāo)簽
path ??????屬性路徑,表示表單對象屬性,如userName、userCode等
cssClass ??表單組件對應(yīng)的CSS樣式類名
cssErrorClass ?當(dāng)提交表單后報(bào)錯(服務(wù)端錯誤),采用的CSS樣式類
cssStyle ??表單組件對應(yīng)的CSS樣式
htmlEscape 綁定的表單屬性值是否要對HTML特殊字符進(jìn)行轉(zhuǎn)換,默認(rèn)為true
注意: 表單組件標(biāo)簽也擁有HTML標(biāo)簽的各種屬性,比如:id、onclick等等,都可以根據(jù)需要,靈活使用
// 跳轉(zhuǎn)到添加頁面add.jsp@RequestMapping("/toaddpage")public String toAddPage(Model model){// 查詢出所有地址Collection<Address> addresses = addressDao.getAddresss();// 添加到請求域中model.addAttribute("address", addresses);// 用于數(shù)據(jù)回顯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><!-- 表單標(biāo)簽:通過SpringMVC的表單標(biāo)簽可以實(shí)現(xiàn)將模型數(shù)據(jù)中的屬性和html表單元素相綁定,以實(shí)現(xiàn)表單數(shù)據(jù)更便捷編輯和表值的回顯.1). SpringMVC認(rèn)為,表單 數(shù)據(jù)中的每一項(xiàng)最終都是要回顯的。path指定的是一個屬性,這個屬性是從隱含模型(請求域中取出的某個對象中屬性)path指定的每一個屬性,請求域中必須有一個對象(這個對象就是請求域中的command對應(yīng)的對象),擁有這個屬性;modelAttribute="",以前我們表單標(biāo)簽會從請求域中獲取一個command對象;把這個對象中的每一個屬性對應(yīng)的顯示出來.modelAttribute="employee",可以告訴SpringMVC不要去取command的值了,我做了一個modelAttribute指定的值;取對象時用的key就是我modelAttribute指定的"employee"--><fm:form action="emp" method="post" modelAttribute="employee"><!-- path就是原來html-input的name值;需要寫path 1).當(dāng)做原生的name項(xiàng)2).自動回顯隱含模型中某個對象對應(yīng)的這個屬性的值-->姓名: <fm:input path="name"/> <br/>性別: 男:<fm:radiobutton path="gender" value="1" /> 女:<fm:radiobutton path="gender" value="0" /><br/><!-- items="": 指定要遍歷的集合;自動遍歷;遍歷出的每一個元素是一個address對象itemLabel="屬性名":指定遍歷出的這個對象的哪個屬性是作為option標(biāo)簽體的值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"/> 女:<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>
?
總結(jié)
以上是生活随笔為你收集整理的springmvc十九:springmvc表单标签的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: springmvc十八:RestfulC
- 下一篇: springmvc二十:数据绑定