spring mvc中的@RequestMapping value用法小結
Spring mvc中@RequestMapping 6個基本用法小結
1)最基本的,方法級別上應用,例如:
@RequestMapping(value="/departments")
public String simplePattern(){
System.out.println("simplePattern method was called");
return "someResult";
}
則訪問http://localhost/xxxx/departments的時候,會呼叫 simplePattern方法了
2) 引數繫結
@RequestMapping(value="/departments")
public String findDepatment(
@RequestParam("departmentId") String departmentId){
System.out.println("Find department with ID: " + departmentId);
return "someResult";
}
形如這樣的訪問形式:
/departments?departmentId=23就可以觸發訪問findDepatment方法了
3 REST風格的引數
@RequestMapping(value="/departments/{departmentId}")
public String findDepatment(@PathVariable String departmentId){
System.out.println("Find department with ID: " + departmentId);
return "someResult";
}
形如REST風格的地址訪問,比如:
/departments/23,其中用(@PathVariable接收rest風格的引數
4 REST風格的引數繫結形式之2
先看例子,這個有點象之前的:
@RequestMapping(value="/departments/{departmentId}")
public String findDepatmentAlternative(
@PathVariable("departmentId") String someDepartmentId){
System.out.println("Find department with ID: " + someDepartmentId);
return "someResult";
}
這個有點不同,就是接收形如/departments/23的URL訪問,把23作為傳入的departmetnId,,但是在實際的方法findDepatmentAlternative中,使用 @PathVariable("departmentId") String someDepartmentId,將其繫結為 someDepartmentId,所以這裡someDepartmentId為23
5 url中同時繫結多個id
@RequestMapping(value="/departments/{departmentId}/employees/{employeeId}")
public String findEmployee(
@PathVariable String departmentId,
@PathVariable String employeeId){
System.out.println("Find employee with ID: " + employeeId +
" from department: " + departmentId);
return "someResult";
}
這個其實也比較好理解了。
6 支援正規表示式
@RequestMapping(value="/{textualPart:[a-z-]+}.{numericPart:[\\d]+}")
public String regularExpression(
@PathVariable String textualPart,
@PathVariable String numericPart){
System.out.println("Textual part: " + textualPart +
", numeric part: " + numericPart);
return "someResult";
}
相關文章
- SpringMVC中@RequestMapping註解中的return “redirect:/“;跟return““的用法。SpringMVCAPP
- @RequestMapping(params = "")用法淺析。APP
- spring@RequestMappingSpringAPP
- MVC 中的@Html.Raw 的用法MVCHTML
- AutoMapper在MVC中的運用小結APPMVC
- sqlldr的用法小結SQL
- js中push(),pop(),unshift(),shift()的用法小結JS
- Java中static、final用法小結Java
- Spring中Value註解的使用Spring
- RMAN中SET命令的常見用法的小結 (ZT)
- webpack用法小結Web
- typedef用法小結
- spring中autowire的用法Spring
- Spring MVC 中 HandlerInterceptorAdapter的使用SpringMVCAPT
- spring 3中新增的@value註解Spring
- Spring MVC 通過 @PropertySource和@Value 來讀取配置檔案SpringMVC
- 【Git】rebase 用法小結Git
- Oracle JOB 用法小結Oracle
- tf.clip_by_value() 用法
- Spring 5 MVC 中的 Router Function 使用SpringMVCFunction
- PHP中ZendCache用法的小例子PHP
- sed基礎用法小結
- 【轉】Oracle JOB 用法小結Oracle
- asp.net中Page.ClientScript.RegisterStartupScript用法小結ASP.NETclient
- Spring MVC xml繫結pojo造成的XXESpringMVCXMLPOJO
- Spring MVC 的Controller 結構問題SpringMVCController
- Spring:一篇文章總結 Spring MVCSpringMVC
- JS/TS 的 import 和 export 用法小結JSImportExport
- Spring 系列(三):你真的懂@RequestMapping嗎?SpringAPP
- ES6 Promise用法小結Promise
- dev treelist 常用用法小結dev
- tkprof命令列工具用法小結命令列
- Spring 小總結Spring
- java中的HashMap用法總結JavaHashMap
- 結構體定義 typedef struct 用法詳解和用法小結結構體Struct
- javascript中關於value的一個小知識點(value既是屬性也是變數)JavaScript變數
- iOS MVC+P+M 個人Note小結iOSMVC
- ASP.NET MVC輔助方法小結ASP.NETMVC