@RequestParam与@PathVariable区别
一、请求样式
使用@RequestParam时,URL是这样的:http://www:port/path?参数名=参数值
使用@PathVariable时,URL是这样的:http://www:port/path/参数值
二、具体用法
- @RequestParam:请求参数,用于请求
Url?id=xxxxxx
路径后面的参数(即id)
当URL使用 Url?id=xxxxxx, 这时的id可通过 @RequestParam注解绑定它传过来的值到方法的参数上。
@RequestMapping("/book")
public void findBook(@RequestParam String id) {
// implementation omitted
}
- @PathVariable
当使用@RequestMapping URI template 样式映射时, 即 someUrl/{paramId}, 这时的paramId可通过 @Pathvariable注解绑定它传过来的值到方法的参数上。
@Controller
@RequestMapping("/owners/{ownerId}")
public class RelativePathUriTemplateController {
@RequestMapping("/pets/{petId}")
public void findPet(@PathVariable String ownerId, @PathVariable String petId, Model model) {
// implementation omitted
}
---------------------
作者:心似一片青苔
来源:优快云
原文:https://blog.youkuaiyun.com/hope_it/article/details/70847432
版权声明:本文为博主原创文章,转载请附上博文链接!
---------------------
作者:心似一片青苔
来源:优快云
原文:https://blog.youkuaiyun.com/hope_it/article/details/70847432
版权声明:本文为博主原创文章,转载请附上博文链接!