亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

SpringMVC實(shí)現(xiàn)數(shù)據(jù)綁定及表單標(biāo)簽

 更新時間:2021年08月02日 11:49:53   作者:MrSaber  
這篇文章主要為大家詳細(xì)介紹了SpringMVC實(shí)現(xiàn)數(shù)據(jù)綁定及表單標(biāo)簽的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下

首先理解數(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>&nbsp;</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)文章

最新評論