首先理解數據綁定
為什么要使用數據綁定
基于http特性,所有的用戶輸入的請求參數類型都是string,比如下面表單:
但我們提交后,為了將請求信息映射到模型中,還需要手動進行格式轉換,此外還借助了一個中轉對象productform,其字段名稱和product一模一樣,只是類型為string。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
@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 { //還需要強制類型轉換 product.setprice( float .parsefloat(productform.getprice())) } catch (exception e) { e.printstacktrace(); } product.setdescription(productform.getdescription()); product savedproduct =productservice.add(product); //這里實現了重定向傳值,但是必須要在配置文件中引用 <annotation-driven/> redirectattributes.addflashattribute( "message" , "the product was successful added" ); return "redirect:/product_view/" +savedproduct.getid(); } |
為了避免轉換異常及減輕我們的工作量,引入了數據綁定。
數據綁定是將用戶輸入綁定到領域模型的一種特性。
有了數據綁定后,springmvc將會為我們自動進行格式轉換,我們如下編寫即可:
1
2
|
public string saveproduct(produc product, redirectattributes redirectattributes) {....} |
這無疑將是方便的。但是,實現數據綁定需要用到表單標簽庫。
表單標簽庫
加入taglib指令
表單標簽庫包含了可以用在jsp頁面中渲染html元素的標簽。
為了使用這些標簽,必須在開頭聲明這個taglib指令
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
表單標簽庫中的所有標簽:
表單標簽
實現的效果
具體的表單標簽的用法,請詳情查看原文章(springmvc表單標簽使用詳解).
下面我僅僅以我的實例,來說明用到的表單標簽:
我們的實現效果:
1.圖書列表界面:
2.圖書編輯界面:
思路分析
1.首先我們在圖書列表界面中,點擊鏈接后,會訪問book_edit/${book.id}。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
<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中。
1
2
3
4
5
6
7
8
9
|
@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.使用表單標簽,綁定requestscope中的book對象和category對象到表單中。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
<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> |
表單標簽之form
使用spring的form標簽主要有兩個作用:
第一是它會自動的綁定來自model中的一個屬性值到當前form對應的實體對象,默認是command屬性,這樣我們就可以在form表單體里面方便的使用該對象的屬性了;但是我們要使用的model中的book,而非默認的command,所以我們可以將保存在model中的book鍵值對的鍵值改為command或者在form中指定commandname,即commandname="book"
第二是它支持我們在提交表單的時候使用除get和post之外的其他方法進行提交,包括delete和put等?!?/p>
1
2
3
4
5
6
7
8
9
10
11
12
13
|
<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> |
說明:
其生成的代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<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在實現除get和post之外的請求方法時,還是使用的post方法進行請求,然后給表單加上了一個隱藏域,用以表示真正的請求方法,這個隱藏域的名稱默認是“_method”。
但此時我們還需要在web.xml中添加:
1
2
3
4
5
6
7
8
|
<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互聯網軟件架構rest
表單標簽之input
springmvc的input標簽會被渲染為一個type為text的普通html input標簽,這個標簽最重要的屬性時path,它將這個輸入字段綁定到book的一個屬性,即綁定到book的標題屬性?!?/p>
1
2
3
4
|
<p> <label for = "title" >title:</label> <form:input id= "title" path= "title" /> </p> |
使用springmvc的input標簽的唯一作用就是它能綁定表單數據。springmvc表單標簽最大的好處就是它支持數據綁定,當我們的表單標簽不需要綁定的數據的時候,我們應該使用普通的html標簽。關于input標簽綁定表單數據的方法已經在介紹form標簽的時候順帶介紹過了,這里就不再過多的贅述了
表單標簽之select
select標簽將會被渲染為一個普通的html select標簽。這里拿user最喜歡的球類運動來做示例,有如下這樣一個處理器方法和對應的視圖頁面:
這個時候會渲染出如下結果:
從上面示例我們可以看出:
1.通過items屬性給select標簽指定了一個數據源,并且綁定了表單對象user的favoriteball屬性。
說明:
items屬性是用于指定當前select的所有可選項的,但是它對于select標簽而言不是必須的,因為我們還可以手動的在select標簽中間加上option標簽來指定select可選的option。
2.select標簽支持的items屬性的數據類型可以是array、collection和map,當數據類型為array或collection時且其中的元素為一個pojo時,我們可以通過屬性itemlabel和itemvalue來指定將用于呈現的option label和value,其他情況下array和collection數據源中的元素將既作為可選項option的value又作為它的label。當items的數據類型為map時,map的key將作為可選項option的value,而map的value將作為option的label標簽。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。