SpringMVC實(shí)現(xiàn)數(shù)據(jù)綁定及表單標(biāo)簽
首先理解數(shù)據(jù)綁定
為什么要使用數(shù)據(jù)綁定
基于HTTP特性,所有的用戶(hù)輸入的請(qǐng)求參數(shù)類(lèi)型都是String,比如下面表單:

但我們提交后,為了將請(qǐng)求信息映射到模型中,還需要手動(dòng)進(jìn)行格式轉(zhuǎn)換,此外還借助了一個(gè)中轉(zhuǎn)對(duì)象productForm,其字段名稱(chēng)和Product一模一樣,只是類(lèi)型為String。
@RequestMapping(value = "/product_save",method = RequestMethod.POST)
public String saveProduct(ProductForm productForm, RedirectAttributes redirectAttributes)
{
logger.info("saveProduct called");
System.out.println(productForm);
Product product = new Product();
product.setName(productForm.getName());
try { //還需要強(qiáng)制類(lèi)型轉(zhuǎn)換
product.setPrice(Float.parseFloat(productForm.getPrice()))
} catch (Exception e) {
e.printStackTrace();
}
product.setDescription(productForm.getDescription());
Product savedProduct =productService.add(product);
//這里實(shí)現(xiàn)了重定向傳值,但是必須要在配置文件中引用 <annotation-driven/>
redirectAttributes.addFlashAttribute("message","The product was successful added");
return "redirect:/product_view/"+savedProduct.getId();
}
為了避免轉(zhuǎn)換異常及減輕我們的工作量,引入了數(shù)據(jù)綁定。
數(shù)據(jù)綁定是將用戶(hù)輸入綁定到領(lǐng)域模型的一種特性。
有了數(shù)據(jù)綁定后,SpringMVC將會(huì)為我們自動(dòng)進(jìn)行格式轉(zhuǎn)換,我們?nèi)缦戮帉?xiě)即可:
public String saveProduct(Produc product, RedirectAttributes redirectAttributes)
{....}
這無(wú)疑將是方便的。但是,實(shí)現(xiàn)數(shù)據(jù)綁定需要用到表單標(biāo)簽庫(kù)。
表單標(biāo)簽庫(kù)
加入taglib指令
表單標(biāo)簽庫(kù)包含了可以用在JSP頁(yè)面中渲染HTML元素的標(biāo)簽。
為了使用這些標(biāo)簽,必須在開(kāi)頭聲明這個(gè)taglib指令
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
表單標(biāo)簽庫(kù)中的所有標(biāo)簽:

表單標(biāo)簽
實(shí)現(xiàn)的效果
具體的表單標(biāo)簽的用法,請(qǐng)?jiān)斍椴榭丛恼?SpringMVC表單標(biāo)簽使用詳解).
下面我僅僅以我的實(shí)例,來(lái)說(shuō)明用到的表單標(biāo)簽:
我們的實(shí)現(xiàn)效果:
1.圖書(shū)列表界面:

2.圖書(shū)編輯界面:

思路分析
1.首先我們?cè)趫D書(shū)列表界面中,點(diǎn)擊鏈接后,會(huì)訪問(wèn)book_edit/${book.id}。
<body>
<a href="<c:url value=" rel="external nofollow" /book_input"/>">Add Book</a>
<table>
<tr>
<th>Category</th>
<th>Title</th>
<th>ISBN</th>
<th>Author</th>
<th> </th>
</tr>
<c:forEach items="${books}" var="book">
<tr>
<td>${book.category.name}</td>
<td>${book.title}</td>
<td>${book.isbn}</td>
<td>${book.author}</td>
<td><a href="book_edit/${book.id}" rel="external nofollow" >Edit</a> </td>
</tr>
</c:forEach>
</table>
</body>
2.Controller接收到請(qǐng)求會(huì)保存類(lèi)別信息和圖書(shū)信息到Model中。
@RequestMapping(value = "/book_edit/{id}")
public String bookSave(Model model, @PathVariable int id)
{
List<Category> categories=bookService.getAllCategorys();
model.addAttribute("categories",categories);
Book book= bookService.get(id);
model.addAttribute("book",book);
return "BookEditForm";
}
3.使用表單標(biāo)簽,綁定requestScope中的Book對(duì)象和Category對(duì)象到表單中。
<body>
<form:form commandName="book" action="book_update" method="post">
<legend>Edit a Book</legend>
<p>
<label for="category">Category:</label>
<form:select id="category" path="category.id" items="${categories}" itemLabel="name" itemValue="id"/>
</p>
<p>
<label for="title">Title:</label>
<form:input id="title" path="title"/>
</p>
<p>
<label for="author">Author:</label>
<form:input id="author" path="author"/>
</p>
<p>
<label for="isbn">ISBN:</label>
<form:input id="title" path="isbn"/>
</p>
<p>
<input type="reset">
<input type="submit" value="Update Book">
</p>
</form:form>
</body>
表單標(biāo)簽之FORM
使用Spring的form標(biāo)簽主要有兩個(gè)作用:
第一是它會(huì)自動(dòng)的綁定來(lái)自Model中的一個(gè)屬性值到當(dāng)前form對(duì)應(yīng)的實(shí)體對(duì)象,默認(rèn)是command屬性,這樣我們就可以在form表單體里面方便的使用該對(duì)象的屬性了;但是我們要使用的Model中的Book,而非默認(rèn)的command,所以我們可以將保存在Model中的Book鍵值對(duì)的鍵值改為command或者在form中指定commandName,即commandName="book"
第二是它支持我們?cè)谔峤槐韱蔚臅r(shí)候使用除GET和POST之外的其他方法進(jìn)行提交,包括DELETE和PUT等?!?/p>
<form:form action="formTag/form.do" method="delete" modelAttribute="user"> <table> <tr> <td>Name:</td><td><form:input path="name"/></td> </tr> <tr> <td>Age:</td><td><form:input path="age"/></td> </tr> <tr> <td colspan="2"><input type="submit" value="提交"/></td> </tr> </table> </form:form>
說(shuō)明:
其生成的代碼如下:
<form id="user" action="formTag/form.do" method="post"> <input type="hidden" name="_method" value="delete"/> <table> <tr> <td>Name:</td><td><input id="name" name="name" type="text" value="ZhangSan"/></td> </tr> <tr> <td>Age:</td><td><input id="age" name="age" type="text" value="36"/></td> </tr> <tr> <td colspan="2"><input type="submit" value="提交"/></td> </tr> </table> </form>
從它生成的代碼我們可以看出,Spring在實(shí)現(xiàn)除GET和POST之外的請(qǐng)求方法時(shí),還是使用的POST方法進(jìn)行請(qǐng)求,然后給表單加上了一個(gè)隱藏域,用以表示真正的請(qǐng)求方法,這個(gè)隱藏域的名稱(chēng)默認(rèn)是“_method”。
但此時(shí)我們還需要在Web.XML中添加:
<filter> <filter-name>hiddenHttpMethodFilter</filter-name> <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class> </filter> <filter-mapping> <filter-name>hiddenHttpMethodFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
詳情請(qǐng)查看:SpringMVC互聯(lián)網(wǎng)軟件架構(gòu)REST
表單標(biāo)簽之Input
SpringMVC的input標(biāo)簽會(huì)被渲染為一個(gè)type為text的普通Html input標(biāo)簽,這個(gè)標(biāo)簽最重要的屬性時(shí)PATH,它將這個(gè)輸入字段綁定到book的一個(gè)屬性,即綁定到Book的標(biāo)題屬性?!?/p>
<p> <label for="title">Title:</label> <form:input id="title" path="title"/> </p>
使用SpringMVC的input標(biāo)簽的唯一作用就是它能綁定表單數(shù)據(jù)。SpringMVC表單標(biāo)簽最大的好處就是它支持?jǐn)?shù)據(jù)綁定,當(dāng)我們的表單標(biāo)簽不需要綁定的數(shù)據(jù)的時(shí)候,我們應(yīng)該使用普通的Html標(biāo)簽。關(guān)于input標(biāo)簽綁定表單數(shù)據(jù)的方法已經(jīng)在介紹form標(biāo)簽的時(shí)候順帶介紹過(guò)了,這里就不再過(guò)多的贅述了
表單標(biāo)簽之Select
select標(biāo)簽將會(huì)被渲染為一個(gè)普通的HTML select標(biāo)簽。這里拿user最喜歡的球類(lèi)運(yùn)動(dòng)來(lái)做示例,有如下這樣一個(gè)處理器方法和對(duì)應(yīng)的視圖頁(yè)面:
這個(gè)時(shí)候會(huì)渲染出如下結(jié)果:

從上面示例我們可以看出:
1.通過(guò)items屬性給select標(biāo)簽指定了一個(gè)數(shù)據(jù)源,并且綁定了表單對(duì)象user的favoriteBall屬性。
說(shuō)明:
Items屬性是用于指定當(dāng)前select的所有可選項(xiàng)的,但是它對(duì)于select標(biāo)簽而言不是必須的,因?yàn)槲覀冞€可以手動(dòng)的在select標(biāo)簽中間加上option標(biāo)簽來(lái)指定select可選的option。
2.Select標(biāo)簽支持的items屬性的數(shù)據(jù)類(lèi)型可以是Array、Collection和Map,當(dāng)數(shù)據(jù)類(lèi)型為Array或Collection時(shí)且其中的元素為一個(gè)POJO時(shí),我們可以通過(guò)屬性itemLabel和itemValue來(lái)指定將用于呈現(xiàn)的option Label和Value,其他情況下Array和Collection數(shù)據(jù)源中的元素將既作為可選項(xiàng)option的value又作為它的Label。當(dāng)items的數(shù)據(jù)類(lèi)型為Map時(shí),Map的key將作為可選項(xiàng)option的value,而Map的value將作為option的Label標(biāo)簽。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
詳解SpringBoot配置devtools實(shí)現(xiàn)熱部署
本篇文章主要介紹了詳解SpringBoot配置devtools實(shí)現(xiàn)熱部署 ,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-05-05
spring mvc實(shí)現(xiàn)文件上傳與下載功能
這篇文章主要為大家詳細(xì)介紹了spring mvc實(shí)現(xiàn)文件上傳與下載功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12
IDEA修改idea64.exe.vmoptions文件以及解決coding卡頓問(wèn)題
IDEA修改idea64.exe.vmoptions文件以及解決coding卡頓問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-04-04
在webservice里調(diào)用耗時(shí)方法出錯(cuò)的解決方案
這篇文章主要介紹了在webservice里調(diào)用耗時(shí)方法出錯(cuò)的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07
詳解Spring Boot 打包分離依賴(lài)JAR 和配置文件
這篇文章主要介紹了Spring Boot 打包分離依賴(lài)JAR 和配置文件,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-11-11
JPA @GeneratedValue 四種標(biāo)準(zhǔn)用法TABLE,SEQUENCE,IDENTITY,
這篇文章主要介紹了@GeneratedValue 四種標(biāo)準(zhǔn)用法TABLE,SEQUENCE,IDENTITY,AUTO詳解,需要的朋友可以參考下2024-03-03
springboot @Value實(shí)現(xiàn)獲取計(jì)算機(jī)中絕對(duì)路徑文件的內(nèi)容
這篇文章主要介紹了springboot @Value實(shí)現(xiàn)獲取計(jì)算機(jī)中絕對(duì)路徑文件的內(nèi)容,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。2021-09-09

