SpringMVC常用注解實例詳解1:@Controller,@RequestMapping,@RequestParam,@PathVariable
2015-01-21 11:39
627 查看
http://www.cnblogs.com/sunang/p/3421707.html
我的開發環境
框架: springmvc+spring+freemarker
開發工具: springsource-tool-suite-2.9.0
JDK版本: 1.6.0_29
tomcat版本:apache-tomcat-7.0.26
前置文章-SpirngMVC配置入門 http://www.cnblogs.com/sunang/p/3419544.html
Spring整合Freemarker http://www.cnblogs.com/sunang/p/3419676.html
本文地址:http://www.cnblogs.com/sunang/p/3421707.html 轉載請注明出處^_^
要注意的點已经用 標注,請大家要特別注意。
1.@Controller,@RequestMapping
用@Controller註釋的類才會被SpringMVC解析器搜索到,這個註釋是必須的。@RequestMapping註釋用於指定controller方法的URL索引。這兩個注解的具體用法請看以下代碼:
controller层LearnMVCController.java代码:
视图learnMVC.ftl代码:
在有父索引的请况下,访问:http://localhost:8080/你的工程名/learnMVC/ex.htm
在省略父索引的请况下,访问:http://localhost:8080/你的工程名/ex.htm 就可将请求发送到所需controller方法。
运行结果:
![](http://images.cnitblog.com/blog/581445/201311/13172911-a603cd6b552f472f99e816df76de06ea.png)
2.@RequestParam
該註釋用於controller方法註釋中,用於綁定由視圖傳過來的參數,代碼如下:
controller代碼
@RequestParam註釋參數中,控制器會掃描視圖傳遞過來的、參數名與value值相同的參數,此處為userId,掃描到參數后將參數的值綁定到註釋後面聲明的變量中,此處為userIdGot.
例如我們訪問地址:http://localhost:8080/你的工程名/learnMVC/ex.htm?userId=10001
那麼首先控制器掃描到URL鏈接後面帶的變量userId與value的值相同,所以控制器把變量userId的值10001賦給了@RequestParam後面聲明的變量userIdGot,這樣我們就得到了前臺傳過來的參數。
required參數為true的時候,如果你沒傳所需的參數,,程序將報錯。required參數可省略,省略時,其值默認為false.
注意:這個例子當中,如果聲明變量Integer userIdGot改成int userIdGot,而你又沒有傳相應的參數,訪問頁面將報錯。因為當控制器找不到匹配的變量時,會把null賦給這個變量,而null值裝化為int的時候會報錯,所以此處應用包裝類。
編寫視圖文件如下:
在瀏覽器訪問:http://localhost:8080/你的工程名/learnMVC/ex.htm?userId=10001
運行結果如下:
![](http://images.cnitblog.com/blog/581445/201311/13193025-986c4590925d4304b05d20ec8d084608.png)
3.@PathVariable
@PathVariable用於綁定URL路徑上添加的參數,例如本文的連接:http://www.cnblogs.com/sunang/p/3421707.html 如果http://www.cnblogs.com/sunang是我們訪問控制層的路徑的話,那麼/p/3421707就是我們添加的參數,通過@PathVariable就可以綁定獲取這些參數在服務端進行相應操作。詳細使用方法情況以下代碼:
首先編寫learnMVC.ftl,代碼如下:
編寫controller層,代碼如下:
@PathVariable的實現原理和@RequestParam相似,控制器掃描@RequestMapping路徑中、{}號裏面的參數名,把參數值綁定到與@PathVariable的value值相等的變量中,比如上例中,{type}的值被綁定到value="type"的註釋後面聲明的變量typeGot中,參數的個數可以自由設定,這裡為兩個參數。
注意:在訪問URL時,必須用ex/參數值/參數值 的格式才能訪問到上例中的controller方法,另外還要注意參數的類型。
現在讓我們運行工程,在瀏覽器訪問:http://localhost:8080/你的工程名/learnMVC/ex/p/10086.htm 注:此處p對應{type},10086對應{articleId}
運行結果:
![](http://images.cnitblog.com/blog/581445/201311/14095337-c1f9cdf6f34449519bbeaf269f50872f.png)
complete!
我的開發環境
框架: springmvc+spring+freemarker
開發工具: springsource-tool-suite-2.9.0
JDK版本: 1.6.0_29
tomcat版本:apache-tomcat-7.0.26
前置文章-SpirngMVC配置入門 http://www.cnblogs.com/sunang/p/3419544.html
Spring整合Freemarker http://www.cnblogs.com/sunang/p/3419676.html
本文地址:http://www.cnblogs.com/sunang/p/3421707.html 轉載請注明出處^_^
要注意的點已经用 標注,請大家要特別注意。
1.@Controller,@RequestMapping
用@Controller註釋的類才會被SpringMVC解析器搜索到,這個註釋是必須的。@RequestMapping註釋用於指定controller方法的URL索引。這兩個注解的具體用法請看以下代碼:
controller层LearnMVCController.java代码:
package www.asuan.com.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller @RequestMapping("/learnMVC")//父索引 可省略 public class LearnMVCController { @RequestMapping("/ex") //子索引 public String learnMVC() { return "learnMVC.ftl"; } }
视图learnMVC.ftl代码:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <h2>Hello World!</h2> </body> </html>
在有父索引的请况下,访问:http://localhost:8080/你的工程名/learnMVC/ex.htm
在省略父索引的请况下,访问:http://localhost:8080/你的工程名/ex.htm 就可将请求发送到所需controller方法。
运行结果:
![](http://images.cnitblog.com/blog/581445/201311/13172911-a603cd6b552f472f99e816df76de06ea.png)
2.@RequestParam
該註釋用於controller方法註釋中,用於綁定由視圖傳過來的參數,代碼如下:
controller代碼
package www.asuan.com.controller; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; @Controller @RequestMapping("/learnMVC") public class LearnMVCController { @RequestMapping("/ex") public String learnMVC(@RequestParam(value = "userId", required = false) Integer userIdGot, Model model) { String str = "成功得到綁定數據:" + userIdGot; model.addAttribute("userIdGot", str); return "learnMVC.ftl"; } }
@RequestParam註釋參數中,控制器會掃描視圖傳遞過來的、參數名與value值相同的參數,此處為userId,掃描到參數后將參數的值綁定到註釋後面聲明的變量中,此處為userIdGot.
例如我們訪問地址:http://localhost:8080/你的工程名/learnMVC/ex.htm?userId=10001
那麼首先控制器掃描到URL鏈接後面帶的變量userId與value的值相同,所以控制器把變量userId的值10001賦給了@RequestParam後面聲明的變量userIdGot,這樣我們就得到了前臺傳過來的參數。
required參數為true的時候,如果你沒傳所需的參數,,程序將報錯。required參數可省略,省略時,其值默認為false.
注意:這個例子當中,如果聲明變量Integer userIdGot改成int userIdGot,而你又沒有傳相應的參數,訪問頁面將報錯。因為當控制器找不到匹配的變量時,會把null賦給這個變量,而null值裝化為int的時候會報錯,所以此處應用包裝類。
編寫視圖文件如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <h2>${userIdGot}</h2> </body> </html>
在瀏覽器訪問:http://localhost:8080/你的工程名/learnMVC/ex.htm?userId=10001
運行結果如下:
![](http://images.cnitblog.com/blog/581445/201311/13193025-986c4590925d4304b05d20ec8d084608.png)
3.@PathVariable
@PathVariable用於綁定URL路徑上添加的參數,例如本文的連接:http://www.cnblogs.com/sunang/p/3421707.html 如果http://www.cnblogs.com/sunang是我們訪問控制層的路徑的話,那麼/p/3421707就是我們添加的參數,通過@PathVariable就可以綁定獲取這些參數在服務端進行相應操作。詳細使用方法情況以下代碼:
首先編寫learnMVC.ftl,代碼如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <h2>${msgGot}</h2> </body> </html>
編寫controller層,代碼如下:
package www.asuan.com.controller; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; @Controller @RequestMapping("/learnMVC") public class LearnMVCController { //子索引由 ex+參數+參數 組成 @RequestMapping("/ex/{type}/{articleId}") public String learnMVC(@PathVariable(value = "type") String typeGot, @PathVariable(value = "articleId") Integer articleIdGot, Model model) { String str = "URL後面所傳輸的參數為:" + typeGot + "/" + articleIdGot; model.addAttribute("msgGot", str); return "learnMVC.ftl"; } }
@PathVariable的實現原理和@RequestParam相似,控制器掃描@RequestMapping路徑中、{}號裏面的參數名,把參數值綁定到與@PathVariable的value值相等的變量中,比如上例中,{type}的值被綁定到value="type"的註釋後面聲明的變量typeGot中,參數的個數可以自由設定,這裡為兩個參數。
注意:在訪問URL時,必須用ex/參數值/參數值 的格式才能訪問到上例中的controller方法,另外還要注意參數的類型。
現在讓我們運行工程,在瀏覽器訪問:http://localhost:8080/你的工程名/learnMVC/ex/p/10086.htm 注:此處p對應{type},10086對應{articleId}
運行結果:
![](http://images.cnitblog.com/blog/581445/201311/14095337-c1f9cdf6f34449519bbeaf269f50872f.png)
complete!
相关文章推荐
- SpringMVC常用注解實例詳解1:@Controller,@RequestMapping,@RequestParam,@PathVariable
- SpringMVC常用注解實例詳解1:@Controller,@RequestMapping,@RequestParam,@PathVariable
- SpringMVC常用注解實例詳解1:@Controller,@RequestMapping,@RequestParam,@PathVariable
- springMVC的@RequestParam注解和@PathVariable注解的区别
- springMVC的注解@RequestParam与@PathVariable的区别
- SpringMVC注解之@RequestParam与@PathVariable
- springmvc的@RequestMapping、@PathVariable、@RequestParam
- springmvc的@RequestMapping、@PathVariable、@RequestParam
- springMVC的@RequestParam注解和@PathVariable注解的区别
- springmvc三大注解标签——@PathVariable @RequestParam @RequestHeader
- springMVC的注解@RequestParam与@PathVariable的区别
- springMVC参数绑定——@RequestParam和@PathVariable注解
- springmvc的@RequestMapping、@PathVariable、@RequestParam
- @RequestMapping注解的简单学习 和 @requestParam @PathVariable
- springMVC注解中@RequestMapping中常用参数value params 以及@RequestParam 详解
- SpringMVC 基于注解的Controller @RequestMapping @RequestParam
- @RequestParam @RequestBody @PathVariable 等参数绑定注解详解
- @RequestParam @RequestBody @PathVariable 等参数绑定注解详解(转)
- 基于注解的Spring MVC(所需jar包,web.xml配置,Spring文件配置,@Controller,@RequestMapping,@RequestParam,model填参,EL取值)
- SpringMVC 基于注解的Controller @RequestMapping @RequestParam..