在mvc結構中,控制器組件主要的功能就是接收請求、處理請求、生成響應,接收客戶端傳來的請求參數的往往是控制器要做的第一件事。
book實體類book.java
1
2
3
4
5
|
public class book { private integer bookid; private string author; //生成get、set方法,此處省略 } |
一、直接用參數名匹配請求參數
客戶端界面(表單):
1
2
3
4
5
|
<form action= "/querystring" method= "post" > <input type= "text" name= "bookid" > <input type= "text" name= "author" > <input type= "submit" value= "提交" > </form> |
controller層:
1
2
3
4
5
6
7
8
9
|
@controller public class parampassdemo { @requestmapping (value= "/querystring" ) public string test1(integer bookid, string author) { system.out.println( "bookid=" +bookid+ ", author=" +author); //此處返回的地址為(/web-inf/jsp/index.jsp) return "index" ; } } |
注意:這里@requestmapping中只有value屬性,value可以省略不寫。
客戶端輸入:123,rose
控制臺輸出:bookid=123, author=rose
二、通過@requestparam注解來指定請求參數的name
客戶端界面(表單):
1
2
3
4
5
|
<form action= "/querystringwithspecname" method= "post" > <input type= "text" name= "bookid" value= "321" > <input type= "text" name= "author" value= "jack" > <input type= "submit" value= "提交" > </form> |
如果表單中的字段與方法中的參數名一致,可以不需要@requestparam,spring會自動處理。
controller層:
1
2
3
4
5
6
7
8
|
@controller public class parampassdemo { @requestmapping ( "/querystringwithspecname" ) public string test2((value= "bookid" ,required= false ) integer id, @requestparam ( "author" ) string name) { system.out.println( "bookid=" +id+ ", author=" +name); return "index" ; } } |
注意:這里@requestmapping中有兩個屬性,value不能省略。
@requestparam將請求地址中的參數傳遞給目標方法,在處理方法入參處使用可以把請求參數傳遞給請求方法。
當使用@requestparam注解時,設置客戶端傳遞的請求參數name="bookid"和@requestparam的value值value="bookid"相匹配后,參數名int id可以和請求參數不匹配。
客戶端輸入:321, jack
控制臺輸出:bookid=321, author=jack
客戶端界面(ajax):
1
2
3
4
5
6
7
8
9
10
11
12
13
|
<button onclick= "clickme()" >點我</button> <script> function clickme() { $.ajax({ type : 'post' , url : "/querystringwithspecname" , data : { "bookid" : 1 , "author" : "jack" }, }); } </script> |
controller層:(不變)
客戶端: data:{"author" : "jack"}
控制臺輸出: bookid=null, author=jack(如果bookid為int類型,控制臺會拋出異常)
客戶端: data:{"bookid" : 1}
控制臺輸出: org.springframework.web.bind.missingservletrequestparameterexception: required string parameter 'author' is not present
通過required設置可選參數,required為false時表示可以不帶參數,為true時表示必須帶參數(默認值為true)。
當可選參數不存在時,spring默認將其賦值為空(null),但由于bookid已定義為基本類型int,所以賦值會失敗。解決方法:采用int包裝類integer。
三、使用領域對象來接收參數
客戶端界面(表單):
1
2
3
4
5
|
<form action= "/querystringwithdomainobj" method= "post" > <input type= "text" name= "bookid" > <input type= "text" name= "author" > <input type= "submit" value= "提交" > </form> |
controller層:
1
2
3
4
5
6
7
8
|
@controller public class parampassdemo { @requestmapping ( "/querystringwithdomainobj" ) public string test3(book book) { system.out.println( "bookid=" +book.getbookid()+ ", author=" +book.getauthor()); return "index" ; } } |
客戶端輸入:111, bob
控制臺輸出:bookid=111, author=bob
四、url動態參數傳遞(路徑參數)
客戶端界面(超鏈接):
1
|
<a href= "/book/1" rel= "external nofollow" >testpathvariable</a> |
controller層:
1
2
3
4
5
6
7
8
9
|
@controller public class parampassdemo { //@pathvariable可以用來映射url中的占位符到目標方法的參數中 @requestmapping ( "/book/{bookid}" ) public string test4( @pathvariable ( "bookid" ) integer bookid) { system.out.println( "bookid:" + bookid); return "index" ; } } |
控制臺輸出:bookid:1
@pathvariable 映射 url 綁定的占位符
通過 @pathvariable 可以將 url 中占位符參數綁定到控制器處理方法的入參中:url 中的 {xxx} 占位符可以通過@pathvariable(“xxx“) 綁定到操作方法的入參中。
五、使用httpservletrequest獲取請求參數
客戶端界面(表單):
1
2
3
4
5
|
<form action= "/querybook" method= "post" > <input type= "text" name= "bookid" > <input type= "text" name= "author" > <input type= "submit" value= "提交" > </form> |
controller層:
1
2
3
4
5
6
7
8
9
|
@controller public class parampassdemo { @requestmapping ( "/querybook" ) public string test5(httpservletrequest request) { system.out.println( "bookid:" + request.getparameter( "bookid" )); //此處index.jsp界面在web-inf下 return "redirect:/index.jsp" ; } } |
客戶端輸入:123
控制臺輸出:用戶id:123
六、跳轉到另一個controller方法
客戶端界面(url地址欄):http://localhost:8080/test6?bookid=321
controller層:
1
2
3
4
5
6
7
8
9
10
|
@controller public class parampassdemo { @requestmapping ( "/test6" ) public string test6(string bookid){ system.out.println( "bookid=" +bookid); //使用服務端跳轉的方式轉向到另一個controller //return "forward:querybook?bookid="+bookid; return "redirect:queryuser?bookid=" +bookid; } } |
控制臺輸出:bookid=321 bookid:321
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:https://segmentfault.com/a/1190000018282239