1. 基本數(shù)據(jù)類型(以int為例,其他類似):
controller代碼:
1
2
3
|
@requestmapping ( "saysth.do" ) public void test( int count) { } |
表單代碼:
1
2
3
4
|
<form action= "saysth.do" method= "post" > <input name= "count" value= "10" type= "text" /> ...... </form> |
表單中input的name值和controller的參數(shù)變量名保持一致,就能完成數(shù)據(jù)綁定,如果不一致可以使用@requestparam注解。需要注意的是,如果controller方法參數(shù)中定義的是基本數(shù)據(jù)類型,但是從頁(yè)面提交過來的數(shù)據(jù)為null或者”"的話,會(huì)出現(xiàn)數(shù)據(jù)轉(zhuǎn)換的異常。
也就是必須保證表單傳遞過來的數(shù)據(jù)不能為null或”",所以,在開發(fā)過程中,對(duì)可能為空的數(shù)據(jù),最好將參數(shù)數(shù)據(jù)類型定義成包裝類型,具體參見下面的例子。
2. 包裝類型(以integer為例,其他類似):
controller代碼:
1
2
3
|
@requestmapping ( "saysth.do" ) public void test(integer count) { } |
表單代碼:
1
2
3
4
|
<form action= "saysth.do" method= "post" > <input name= "count" value= "10" type= "text" /> ...... </form> |
和基本數(shù)據(jù)類型基本一樣,不同之處在于,表單傳遞過來的數(shù)據(jù)可以為null或”",以上面代碼為例,如果表單中num為”"或者表單中無num這個(gè)input,那么,controller方法參數(shù)中的num值則為null。
3. 自定義對(duì)象類型:
model代碼:
1
2
3
4
5
6
|
public class user { private string firstname; private string lastname; 省略set,get 方法 } |
controller代碼:
1
2
3
|
@requestmapping ( "saysth.do" ) public void test(user user) { } |
表單代碼:
1
2
3
4
5
|
<form action= "saysth.do" method= "post" > <input name= "firstname" value= "張" type= "text" /> <input name= "lastname" value= "三" type= "text" /> ...... </form> |
非常簡(jiǎn)單,只需將對(duì)象的屬性名和input的name值一一匹配即可。
4. 自定義復(fù)合對(duì)象類型:
model代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
public class contactinfo { private string tel; private string address; 省略set ,get } public class user { private string firstname; private string lastname; private contactinfo contactinfo; 省略set ,get } |
controller代碼:
1
2
3
4
5
6
7
|
@requestmapping ( "saysth.do" ) public void test(user user) { system.out.println(user.getfirstname()); system.out.println(user.getlastname()); system.out.println(user.getcontactinfo().gettel()); system.out.println(user.getcontactinfo().getaddress()); } |
表單代碼:
1
2
3
4
5
6
7
|
<form action= "saysth.do" method= "post" > <input name= "firstname" value= "張" /><br> <input name= "lastname" value= "三" /><br> <input name= "contactinfo.tel" value= "13809908909" /><br> <input name= "contactinfo.address" value= "北京海淀" /><br> <input type= "submit" value= "save" /> </form> |
user對(duì)象中有contactinfo屬性,controller中的代碼和第3點(diǎn)說的一致,但是,在表單代碼中,需要使用“屬性名(對(duì)象類型的屬性).屬性名”來命名input的name。
5. list綁定:
list需要綁定在對(duì)象上,而不能直接寫在controller方法的參數(shù)中。
model代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
public class user { private string firstname; private string lastname; 省略set ,get } public class userlistform { private list<user> users; public list<user> getusers() { return users; } public void setusers(list<user> users) { this .users = users; } } |
controller代碼:
1
2
3
4
5
6
|
@requestmapping ( "saysth.do" ) public void test(userlistform userform) { for (user user : userform.getusers()) { system.out.println(user.getfirstname() + " - " + user.getlastname()); } } |
表單代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
<form action= "saysth.do" method= "post" > <input type= "submit" value= "save" /></td> </tr> </tfoot> <tbody> <tr> <td><input name= "users[0].firstname" value= "aaa" /></td> <td><input name= "users[0].lastname" value= "bbb" /></td> </tr> <tr> <td><input name= "users[1].firstname" value= "ccc" /></td> <td><input name= "users[1].lastname" value= "ddd" /></td> </tr> <tr> <td><input name= "users[2].firstname" value= "eee" /></td> <td><input name= "users[2].lastname" value= "fff" /></td> </tr> </form> |
其實(shí),這和第4點(diǎn)user對(duì)象中的contantinfo數(shù)據(jù)的綁定有點(diǎn)類似,但是這里的userlistform對(duì)象里面的屬性被定義成list,而不是普通自定義對(duì)象。
所以,在表單中需要指定list的下標(biāo)。值得一提的是,spring會(huì)創(chuàng)建一個(gè)以最大下標(biāo)值為size的list對(duì)象,所以,如果表單中有動(dòng)態(tài)添加行、刪除行的情況,就需要特別注意,譬如一個(gè)表格,用戶在使用過程中經(jīng)過多次刪除行、增加行的操作之后,下標(biāo)值就會(huì)與實(shí)際大小不一致,
這時(shí)候,list中的對(duì)象,只有在表單中對(duì)應(yīng)有下標(biāo)的那些才會(huì)有值,否則會(huì)為null,看個(gè)例子:
表單代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
<form action= "saysth.do" method= "post" > <td colspan= "2" ><input type= "submit" value= "save" /></td> </tr> </tfoot> <tbody> <tr> <td><input name= "users[0].firstname" value= "aaa" /></td> <td><input name= "users[0].lastname" value= "bbb" /></td> </tr> <tr> <td><input name= "users[1].firstname" value= "ccc" /></td> <td><input name= "users[1].lastname" value= "ddd" /></td> </tr> <tr> <td><input name= "users[20].firstname" value= "eee" /></td> <td><input name= "users[20].lastname" value= "fff" /></td> </form> |
這個(gè)時(shí)候,controller中的userform.getusers()獲取到list的size為21,而且這21個(gè)user對(duì)象都不會(huì)為null,但是,第2到第19的user對(duì)象中的firstname和lastname都為null。打印結(jié)果:
1
2
3
4
5
6
7
|
aaa - bbb ccc - ddd null - null null - null ..... null - null eee - fff |
6. set綁定:
set和list類似,也需要綁定在對(duì)象上,而不能直接寫在controller方法的參數(shù)中。但是,綁定set數(shù)據(jù)時(shí),必須先在set對(duì)象中add相應(yīng)的數(shù)量的模型對(duì)象。
model代碼:
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
|
public class user { private string firstname; private string lastname; 省略set ,get } public class usersetform { private set<user> users = new hashset<user>(); public usersetform() { users.add( new user()); users.add( new user()); users.add( new user()); } public set<user> getusers() { return users; } public void setusers(set<user> users) { this .users = users; } } |
controller代碼:
1
2
3
4
5
6
|
@requestmapping ( "saysth.do" ) public void test(usersetform userform) { for (user user : userform.getusers()) { system.out.println(user.getfirstname() + " - " + user.getlastname()); } } |
表單代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
<form action= "saysth.do" method= "post" > <input type= "submit" value= "save" /></td> </tr> </tfoot> <tbody> <tr> <td><input name= "users[0].firstname" value= "aaa" /></td> <td><input name= "users[0].lastname" value= "bbb" /></td> </tr> <tr> <td><input name= "users[1].firstname" value= "ccc" /></td> <td><input name= "users[1].lastname" value= "ddd" /></td> </tr> <tr> <td><input name= "users[2].firstname" value= "eee" /></td> <td><input name= "users[2].lastname" value= "fff" /></td> </form> |
基本和list綁定類似。
需要特別提醒的是,如果最大下標(biāo)值大于set的size,則會(huì)拋出org.springframework.beans.invalidpropertyexception異常。所以,在使用時(shí)有些不便。
7. map綁定:
map最為靈活,它也需要綁定在對(duì)象上,而不能直接寫在controller方法的參數(shù)中。
model代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
public class user { private string firstname; private string lastname; 省略set ,get } public class usermapform { private map<string, user> users; public map<string, user> getusers() { return users; } public void setusers(map<string, user> users) { this .users = users; } } |
controller代碼:
1
2
3
4
5
6
7
|
@requestmapping ( "saysth.do" ) public void test(usermapform userform) { for (map.entry<string, user> entry : userform.getusers().entryset()) { system.out.println(entry.getkey() + ": " + entry.getvalue().getfirstname() + " - " + entry.getvalue().getlastname()); } } |
表單代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
<form action= "saysth.do" method= "post" > <input type= "submit" value= "save" /></td> </tr> </tfoot> <tbody> <tr> <td><input name= "users['x'].firstname" value= "aaa" /></td> <td><input name= "users['x'].lastname" value= "bbb" /></td> </tr> <tr> <td><input name= "users['y'].firstname" value= "ccc" /></td> <td><input name= "users['y'].lastname" value= "ddd" /></td> </tr> <tr> <td><input name= "users['z'].firstname" value= "eee" /></td> <td><input name= "users['z'].lastname" value= "fff" /></td> </form> |
以上這篇springmvc前臺(tái)向后臺(tái)傳值幾種方式總結(jié)(從簡(jiǎn)單到復(fù)雜)就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持服務(wù)器之家。
原文鏈接:https://blog.csdn.net/revent/article/details/64125965