Spring MVC中@PathVariable和@RequestParam的区别

Spring MVC中,两者的作用都是将request里的参数的值绑定到contorl里的方法参数里的,区别在于,URL写法不同。

使用@RequestParam时,URL是这样的:http://host:port/path?参数名=参数值

使用@PathVariable时,URL是这样的:http://host:port/path/参数值

例如:

 @RequestMapping(value = "/{empo}",method = RequestMethod.GET)
 public Emp one(@PathVariable(value = "empo",required=false) Long empno){
   System.out.println("我进来了");
   return iEmpService.selectById(empno);
 }

 访问的路径是:127.0.0.1:8080/1

@RequestMapping(value = "/emp",method = RequestMethod.GET)
public Emp one(@RequestParam(value = "empo",required=false) Long empno){
  System.out.println("我进来了");
  return iEmpService.selectById(empno);
}

 访问的路径是:127.0.0.1:8080/emp?empo=1

你可能感兴趣的:(java)