SpringMVC實(shí)現(xiàn)數(shù)據(jù)綁定及表單標(biāo)簽
首先理解數(shù)據(jù)綁定
為什么要使用數(shù)據(jù)綁定
基于HTTP特性,所有的用戶輸入的請求參數(shù)類型都是String,比如下面表單:
但我們提交后,為了將請求信息映射到模型中,還需要手動進(jìn)行格式轉(zhuǎn)換,此外還借助了一個中轉(zhuǎn)對象productForm,其字段名稱和Product一模一樣,只是類型為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)制類型轉(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ù)綁定是將用戶輸入綁定到領(lǐng)域模型的一種特性。
有了數(shù)據(jù)綁定后,SpringMVC將會為我們自動進(jìn)行格式轉(zhuǎn)換,我們?nèi)缦戮帉懠纯桑?/p>
public String saveProduct(Produc product, RedirectAttributes redirectAttributes) {....}
這無疑將是方便的。但是,實(shí)現(xiàn)數(shù)據(jù)綁定需要用到表單標(biāo)簽庫。
表單標(biāo)簽庫
加入taglib指令
表單標(biāo)簽庫包含了可以用在JSP頁面中渲染HTML元素的標(biāo)簽。
為了使用這些標(biāo)簽,必須在開頭聲明這個taglib指令
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
表單標(biāo)簽庫中的所有標(biāo)簽:
表單標(biāo)簽
實(shí)現(xiàn)的效果
具體的表單標(biāo)簽的用法,請詳情查看原文章(SpringMVC表單標(biāo)簽使用詳解).
下面我僅僅以我的實(shí)例,來說明用到的表單標(biāo)簽:
我們的實(shí)現(xiàn)效果:
1.圖書列表界面:
2.圖書編輯界面:
思路分析
1.首先我們在圖書列表界面中,點(diǎ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接收到請求會保存類別信息和圖書信息到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對象和Category對象到表單中。
<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)簽主要有兩個作用:
第一是它會自動的綁定來自Model中的一個屬性值到當(dāng)前form對應(yīng)的實(shí)體對象,默認(rèn)是command屬性,這樣我們就可以在form表單體里面方便的使用該對象的屬性了;但是我們要使用的Model中的Book,而非默認(rèn)的command,所以我們可以將保存在Model中的Book鍵值對的鍵值改為command或者在form中指定commandName,即commandName="book"
第二是它支持我們在提交表單的時候使用除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>
說明:
其生成的代碼如下:
<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之外的請求方法時,還是使用的POST方法進(jìn)行請求,然后給表單加上了一個隱藏域,用以表示真正的請求方法,這個隱藏域的名稱默認(rèn)是“_method”。
但此時我們還需要在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>
詳情請查看:SpringMVC互聯(lián)網(wǎng)軟件架構(gòu)REST
表單標(biāo)簽之Input
SpringMVC的input標(biāo)簽會被渲染為一個type為text的普通Html input標(biāo)簽,這個標(biāo)簽最重要的屬性時PATH,它將這個輸入字段綁定到book的一個屬性,即綁定到Book的標(biāo)題屬性。
<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ù)的時候,我們應(yīng)該使用普通的Html標(biāo)簽。關(guān)于input標(biāo)簽綁定表單數(shù)據(jù)的方法已經(jīng)在介紹form標(biāo)簽的時候順帶介紹過了,這里就不再過多的贅述了
表單標(biāo)簽之Select
select標(biāo)簽將會被渲染為一個普通的HTML select標(biāo)簽。這里拿user最喜歡的球類運(yùn)動來做示例,有如下這樣一個處理器方法和對應(yīng)的視圖頁面:
這個時候會渲染出如下結(jié)果:
從上面示例我們可以看出:
1.通過items屬性給select標(biāo)簽指定了一個數(shù)據(jù)源,并且綁定了表單對象user的favoriteBall屬性。
說明:
Items屬性是用于指定當(dāng)前select的所有可選項(xiàng)的,但是它對于select標(biāo)簽而言不是必須的,因?yàn)槲覀冞€可以手動的在select標(biāo)簽中間加上option標(biāo)簽來指定select可選的option。
2.Select標(biāo)簽支持的items屬性的數(shù)據(jù)類型可以是Array、Collection和Map,當(dāng)數(shù)據(jù)類型為Array或Collection時且其中的元素為一個POJO時,我們可以通過屬性itemLabel和itemValue來指定將用于呈現(xiàn)的option Label和Value,其他情況下Array和Collection數(shù)據(jù)源中的元素將既作為可選項(xiàng)option的value又作為它的Label。當(dāng)items的數(shù)據(jù)類型為Map時,Map的key將作為可選項(xiàng)option的value,而Map的value將作為option的Label標(biāo)簽。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java實(shí)現(xiàn)獲取客戶端真實(shí)IP方法小結(jié)
本文給大家匯總介紹了2種使用java實(shí)現(xiàn)獲取客戶端真實(shí)IP的方法,主要用于獲取使用了代理訪問的來訪者的IP,有需要的小伙伴可以參考下。2016-03-03數(shù)據(jù)庫CURD必備搭檔mybatis?plus詳解
這篇文章主要為大家介紹了數(shù)據(jù)庫CURD必備搭檔mybatis?plus詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05Java基于IDEA實(shí)現(xiàn)http編程的示例代碼
這篇文章主要介紹了Java基于IDEA實(shí)現(xiàn)http編程的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04Spring3 MVC請求參數(shù)獲取的幾種方法小結(jié)
本篇文章主要介紹了Spring3 MVC請求參數(shù)獲取的幾種方法小結(jié),非常具有實(shí)用價值,需要的朋友可以參考下。2017-03-03基于selenium-java封裝chrome、firefox、phantomjs實(shí)現(xiàn)爬蟲
這篇文章主要介紹了基于selenium-java封裝chrome、firefox、phantomjs實(shí)現(xiàn)爬蟲,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2020-10-10JDK8配置環(huán)境變量的bat文件的詳細(xì)教程
這篇文章主要介紹了JDK8配置環(huán)境變量的bat文件,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-07-07Spring的兩種事務(wù)管理機(jī)制的基本概念和demo示例
Spring事務(wù)包括聲明式事務(wù)管理和注解式事務(wù)管理,我們通過概念和小demo的形式一步一步地來一起學(xué)習(xí)這個知識點(diǎn),需要的朋友可以參考下2023-07-07