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
- spring@RequestMappingSpringAPP
- MVC 中的@Html.Raw 的用法MVCHTML
- js中push(),pop(),unshift(),shift()的用法小結JS
- webpack用法小結Web
- Spring MVC xml繫結pojo造成的XXESpringMVCXMLPOJO
- Spring 5 MVC 中的 Router Function 使用SpringMVCFunction
- Spring 系列(三):你真的懂@RequestMapping嗎?SpringAPP
- Spring Boot中用@RequestMapping 提供 Zip 檔案Spring BootAPP
- Spring:一篇文章總結 Spring MVCSpringMVC
- tf.clip_by_value() 用法
- Apr.7.2024小結——彙編中jmp和call的用法
- Spring MVC 中 HttpMessageConverter 轉換器SpringMVCHTTP
- Solon MVC 的 @Mapping 用法說明MVCAPP
- spring - mvcSpringMVC
- spring、spring MVC、spring BootMVCSpring Boot
- PHP中ZendCache用法的小例子PHP
- tomcat + spring mvc原理外傳:spring mvc與前端的糾葛TomcatSpringMVC前端
- JS/TS 的 import 和 export 用法小結JSImportExport
- Spring 小總結Spring
- spring mvc中獲取請求URLSpringMVC
- Spring 系列(二):Spring MVC的父子容器SpringMVC
- ES6 Promise用法小結Promise
- java中的HashMap用法總結JavaHashMap
- spring mvc interceptorsSpringMVC
- spring - mvc - @ScheduledSpringMVC
- spring - mvc - @ValidSpringMVC
- Spring MVC整合redis的配置SpringMVCRedis
- Eclipse Spring MVC工程的建立EclipseSpringMVC
- spring-mvc的工作原理SpringMVC
- Spring歸納小結Spring
- Spring之Config小結Spring
- Spring MVC 到 Spring Boot 的簡化之路MVCSpring Boot
- Spring MVC和Spring Boot的區別 - hackernoonMVCSpring Boot
- @RequestMapping的三大特性APP
- Spring Boot + Mybatis + Spring MVC環境配置(四):MVC框架搭建Spring BootMyBatisMVC框架
- 第63節:Java中的Spring MVC簡介筆記JavaSpringMVC筆記
- 說說 Spring AOP 中 @Aspect 的高階用法Spring