1. 基本數據類型(以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的參數變量名保持一致,就能完成數據綁定,如果不一致可以使用@requestparam注解。需要注意的是,如果controller方法參數中定義的是基本數據類型,但是從頁面提交過來的數據為null或者”"的話,會出現數據轉換的異常。
也就是必須保證表單傳遞過來的數據不能為null或”",所以,在開發過程中,對可能為空的數據,最好將參數數據類型定義成包裝類型,具體參見下面的例子。
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> |
和基本數據類型基本一樣,不同之處在于,表單傳遞過來的數據可以為null或”",以上面代碼為例,如果表單中num為”"或者表單中無num這個input,那么,controller方法參數中的num值則為null。
3. 自定義對象類型:
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> |
非常簡單,只需將對象的屬性名和input的name值一一匹配即可。
4. 自定義復合對象類型:
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對象中有contactinfo屬性,controller中的代碼和第3點說的一致,但是,在表單代碼中,需要使用“屬性名(對象類型的屬性).屬性名”來命名input的name。
5. list綁定:
list需要綁定在對象上,而不能直接寫在controller方法的參數中。
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> |
其實,這和第4點user對象中的contantinfo數據的綁定有點類似,但是這里的userlistform對象里面的屬性被定義成list,而不是普通自定義對象。
所以,在表單中需要指定list的下標。值得一提的是,spring會創建一個以最大下標值為size的list對象,所以,如果表單中有動態添加行、刪除行的情況,就需要特別注意,譬如一個表格,用戶在使用過程中經過多次刪除行、增加行的操作之后,下標值就會與實際大小不一致,
這時候,list中的對象,只有在表單中對應有下標的那些才會有值,否則會為null,看個例子:
表單代碼:
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> |
這個時候,controller中的userform.getusers()獲取到list的size為21,而且這21個user對象都不會為null,但是,第2到第19的user對象中的firstname和lastname都為null。打印結果:
1
2
3
4
5
6
7
|
aaa - bbb ccc - ddd null - null null - null ..... null - null eee - fff |
6. set綁定:
set和list類似,也需要綁定在對象上,而不能直接寫在controller方法的參數中。但是,綁定set數據時,必須先在set對象中add相應的數量的模型對象。
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綁定類似。
需要特別提醒的是,如果最大下標值大于set的size,則會拋出org.springframework.beans.invalidpropertyexception異常。所以,在使用時有些不便。
7. map綁定:
map最為靈活,它也需要綁定在對象上,而不能直接寫在controller方法的參數中。
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前臺向后臺傳值幾種方式總結(從簡單到復雜)就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持服務器之家。
原文鏈接:https://blog.csdn.net/revent/article/details/64125965