springMVC笔记系列——RequestParam注解
前面的文章介紹過(guò)注解@PathVariable,它能夠?yàn)镽est風(fēng)格的URL用占位符的方式傳遞一個(gè)參數(shù),但是這個(gè)參數(shù)并不是真正意義上的請(qǐng)求參數(shù)。請(qǐng)求參數(shù)怎么處理是本文的主要內(nèi)容。
Spring MVC 通過(guò)分析處理方法的簽名,將 HTTP 請(qǐng)求信息綁定到處理方法的相應(yīng)人參中。
Spring MVC 對(duì)控制器處理方法簽名的限制是很寬松的,幾乎可以按喜歡的任何方式對(duì)方法進(jìn)行簽名。
必要時(shí)可以對(duì)方法及方法入?yún)?biāo)注相應(yīng)的注解(@PathVariable、 @RequestParam、 @RequestHeader 等)、 SpringMVC 框架會(huì)將 HTTP 請(qǐng)求的信息綁定到相應(yīng)的方法入?yún)⒅?#xff0c;并根據(jù)方法的返回值類型做出相應(yīng)的后續(xù)處理。
(本文出自:http://my.oschina.net/happyBKs/blog/417032)
在處理方法入?yún)⑻幨褂?@RequestParam 可以把請(qǐng)求參數(shù)傳遞給請(qǐng)求方法
– value:參數(shù)名
– required:是否必須。默認(rèn)為 true, 表示請(qǐng)求參數(shù)中必須包含對(duì)應(yīng)的參數(shù),若不存在,將拋出異常
控制器類和處理函數(shù)如下:
package?com.happyBKs.springmvc.handlers;import?org.springframework.stereotype.Controller; import?org.springframework.web.bind.annotation.RequestMapping; import?org.springframework.web.bind.annotation.RequestParam;@RequestMapping("class") @Controller public?class?RPTestHandler?{String?page="successrm";@RequestMapping("student")public?String?handle(@RequestParam(value="username")?String?un,?@RequestParam(value="age")?int?age){System.out.println("a?student's?request?has?come.?username:?"+un+",?age:?"+age);return?page;}}這里使用@RequestParam注解的value屬性值來(lái)映射請(qǐng)求參數(shù)。
請(qǐng)求頁(yè)面index8.jsp:
<%@?page?language="java"?contentType="text/html;?charset=ISO-8859-1"pageEncoding="ISO-8859-1"%> <html> <head> <meta?http-equiv="Content-Type"?content="text/html;?charset=ISO-8859-1"> <title>Insert?title?here</title> </head> <body> <a?href="class/student?username=happyBKs&age=100">class/student?username=happyBKs&age=100</a> </body> </html>好,運(yùn)行過(guò)程如下:
點(diǎn)擊超攔截請(qǐng)求:
控制臺(tái)此時(shí)輸出:
a student's request has come. username: happyBKs, age: 100
也可以直接在瀏覽器地址欄輸入http://localhost:8080/mymvc/class/student?username=happyBKs&age=101
控制臺(tái)顯示 ??a student's request has come. username: happyBKs, age: 101
問題1:如果我們的請(qǐng)求少一個(gè)參數(shù)age,會(huì)怎么樣?
這個(gè)問題該如何解決呢?
這里需要用到@RequestParam注解的required屬性或defaultValue屬性。
required屬性標(biāo)注這個(gè)參數(shù)是否是必需大的,默認(rèn)是true,如果想讓它可以不存在,那么就設(shè)置為false。但是請(qǐng)注意,required設(shè)置成false的參數(shù)對(duì)應(yīng)的處理函數(shù)的參數(shù)類型必須是對(duì)象類型,否則回報(bào)錯(cuò)500!
例如這里我們?nèi)绻麑⒖刂破黝惛某?#xff1a;
package?com.happyBKs.springmvc.handlers;import?org.springframework.stereotype.Controller; import?org.springframework.web.bind.annotation.RequestMapping; import?org.springframework.web.bind.annotation.RequestParam;@RequestMapping("class") @Controller public?class?RPTestHandler?{String?page="successrm";@RequestMapping("student")public?String?handle(@RequestParam(value="username")?String?un,?@RequestParam(value="age",required=false)?int?age){System.out.println("a?student's?request?has?come.?username:?"+un+",?age:?"+age);return?page;}}結(jié)果會(huì)報(bào)錯(cuò),因?yàn)閍ge雖然設(shè)置了required為false,請(qǐng)求參數(shù)可以不包含age,但是在處理函數(shù)中對(duì)應(yīng)的參數(shù)類型是int,int是基本數(shù)據(jù)類型,無(wú)法為空,所以報(bào)錯(cuò)。解決辦法是將int改為Integer。
運(yùn)行結(jié)果:
控制臺(tái)輸出:
a student's request has come. username: happyBKs, age: null
如果我們?nèi)匀幌胗没绢愋妥鳛閰?shù)類型,那么可以用到@RequestParam注解的defaultValue屬性來(lái)指定默認(rèn)值。
package?com.happyBKs.springmvc.handlers;import?org.springframework.stereotype.Controller; import?org.springframework.web.bind.annotation.RequestMapping; import?org.springframework.web.bind.annotation.RequestParam;@RequestMapping("class") @Controller public?class?RPTestHandler?{String?page="successrm";@RequestMapping("student")public?String?handle(@RequestParam(value="username")?String?un,?@RequestParam(value="age",required=false,?defaultValue="0")?int?age){System.out.println("a?student's?request?has?come.?username:?"+un+",?age:?"+age);return?page;}}運(yùn)行結(jié)果:
控制臺(tái)?a student's request has come. username: happyBKs, age: 0
最后,做個(gè)總結(jié):(@RequestParam注解是很常用的,所以很重要)
?* @RequestParam 來(lái)映射請(qǐng)求參數(shù)
?* value值 即請(qǐng)求參數(shù)名
?* required 該參數(shù)是否必需。默認(rèn)為true
?* defaultValue請(qǐng)求參數(shù)的默認(rèn)值
總結(jié)
以上是生活随笔為你收集整理的springMVC笔记系列——RequestParam注解的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 方立勋_30天掌握JavaWeb_Ses
- 下一篇: java四大域总结