SpringMVC系列(3)之@RequestParam与@PathVariable区别

来源:互联网 发布:苹果屏幕录像软件 编辑:程序博客网 时间:2024/06/06 13:05

@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     }  
0 0