在mvc結(jié)構(gòu)中,控制器組件主要的功能就是接收請求、處理請求、生成響應(yīng),接收客戶端傳來的請求參數(shù)的往往是控制器要做的第一件事。
book實(shí)體類book.java
1
2
3
4
5
|
public class book { private integer bookid; private string author; //生成get、set方法,此處省略 } |
一、直接用參數(shù)名匹配請求參數(shù)
客戶端界面(表單):
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注解來指定請求參數(shù)的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> |
如果表單中的字段與方法中的參數(shù)名一致,可以不需要@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將請求地址中的參數(shù)傳遞給目標(biāo)方法,在處理方法入?yún)⑻幨褂每梢园颜埱髤?shù)傳遞給請求方法。
當(dāng)使用@requestparam注解時,設(shè)置客戶端傳遞的請求參數(shù)name="bookid"和@requestparam的value值value="bookid"相匹配后,參數(shù)名int id可以和請求參數(shù)不匹配。
客戶端輸入:321, jack
控制臺輸出:bookid=321, author=jack
客戶端界面(ajax):
1
2
3
4
5
6
7
8
9
10
11
12
13
|
<button onclick= "clickme()" >點(diǎn)我</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設(shè)置可選參數(shù),required為false時表示可以不帶參數(shù),為true時表示必須帶參數(shù)(默認(rèn)值為true)。
當(dāng)可選參數(shù)不存在時,spring默認(rèn)將其賦值為空(null),但由于bookid已定義為基本類型int,所以賦值會失敗。解決方法:采用int包裝類integer。
三、使用領(lǐng)域?qū)ο髞斫邮諈?shù)
客戶端界面(表單):
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動態(tài)參數(shù)傳遞(路徑參數(shù))
客戶端界面(超鏈接):
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中的占位符到目標(biāo)方法的參數(shù)中 @requestmapping ( "/book/{bookid}" ) public string test4( @pathvariable ( "bookid" ) integer bookid) { system.out.println( "bookid:" + bookid); return "index" ; } } |
控制臺輸出:bookid:1
@pathvariable 映射 url 綁定的占位符
通過 @pathvariable 可以將 url 中占位符參數(shù)綁定到控制器處理方法的入?yún)⒅校簎rl 中的 {xxx} 占位符可以通過@pathvariable(“xxx“) 綁定到操作方法的入?yún)⒅小?/p>
五、使用httpservletrequest獲取請求參數(shù)
客戶端界面(表單):
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
六、跳轉(zhuǎn)到另一個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); //使用服務(wù)端跳轉(zhuǎn)的方式轉(zhuǎn)向到另一個controller //return "forward:querybook?bookid="+bookid; return "redirect:queryuser?bookid=" +bookid; } } |
控制臺輸出:bookid=321 bookid:321
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:https://segmentfault.com/a/1190000018282239