springmvc可以通過requestparam注解來映射獲得參數,具體用法如下:
例子:
配置過程省略
1.新建controller類
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
package com.loger.controller; import org.springframework.stereotype.controller; import org.springframework.web.bind.annotation.requestmapping; @controller public class requestparam { public static final string success = "success" ; @requestmapping (value= "/requestparam" ) public string requestparam( @org .springframework.web.bind.annotation. requestparam(value= "username" ) string un, @org .springframework.web.bind.annotation.requestparam(value= "age" ) integer age){ system.out.println(un + " " + age); return success; } } |
2.index.jsp
運行結果:
補充:如果表單名跟方法的參數名一致的話,無需再用@requestparam注解來映射。
如改為
@requestmapping(value="/requestparam")
public string requestparam(string username,integer age)即可!
用類作為參數,且包含級聯屬性的參數獲取方法:
1.新建adress類
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
package com.loger.bean; public class address { private string province; private string city; public string getprovince() { return province; } public void setprovince(string province) { this .province = province; } public string getcity() { return city; } public void setcity(string city) { this .city = city; } @override public string tostring() { return "address [province=" + province + ", city=" + city + "]" ; } } |
2.新建user類
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
26
27
28
29
30
|
package com.loger.bean; public class user { private string name; private int age; private address address; public string getname() { return name; } public void setname(string name) { this .name = name; } public int getage() { return age; } public void setage( int age) { this .age = age; } public address getaddress() { return address; } public void setaddress(address address) { this .address = address; } @override public string tostring() { return "user [name=" + name + ", age=" + age + ", address=" + address + "]" ; } } |
3.controller
4.表單
user有級聯屬性address,表單傳入的參數是address.city address.province
1
2
3
4
5
6
7
|
<form action= "pojoparam" > 姓名:<input type= "text" name= "name" ><br> 年齡:<input type= "text" name= "age" ><br> 城市:<input type= "text" name= "address.city" ><br> 省份:<input type= "text" name= "address.province" ><br> <input type= "submit" value= "提交" ><br> </form> |
運行結果:
以上這篇springmvc通過注解獲得參數的實例就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持服務器之家。
原文鏈接:http://www.cnblogs.com/loger1995/p/6274717.html