Reprinted from: https://blog.csdn.net/chuck_kui/article/details/55506723
First two addresses:
Address①http://localhost:8989/SSSP/emps?pageNo=2
address②http://localhost:8989/SSSP/emp/7
If you want to get the value '2' of pageNo in address ①, use @RequestParam ,
If you want to get '7' in emp/7 in address ② use @PathVariable
The method used to obtain '2' in address ① is as follows
- @RequestMapping("/emps")
- public String list(@RequestParam(value="pageNo",required=false,
- defaultValue="1")String pageNoStr,Map<String, Object>map){
- int pageNo = 1;
- try {
- //Check for pageNo
- pageNo = Integer.parseInt(pageNoStr);
- if(pageNo<1){
- pageNo = 1;
- }
- } catch (Exception e) {}
- Page<Employee> page = employeeService.getPage(pageNo, 5);
- map.put("page",page);
- return"emp/list";
- }
The method used to obtain '7' in address ② is as follows:
- @RequestMapping(value="/emp/{id}",method=RequestMethod.GET)
- public String edit(@PathVariable("id")Integer id,Map<String , Object>map){
- Employee employee = employeeService.getEmployee(id);
- List<Department> departments = departmentService.getAll();
- map.put("employee", employee);
- map.put("departments", departments);
- return "emp/input";
- }
大道理不讲 原理也不分析就记忆一点,那一点呢? 看‘这个符号‘?’
1. 若获取的入参的 参数 是下面这种形式 就使用 @requestParam 去获取 参数‘2’
/emps?pageNo=2
2. 若获取的入参的 参数 是下面这种形式 就使用 @PathVariable 去获取参数 ‘7’
/emp/7
多说一点,拽一下
RequestParam 汉语意思就是: 请求参数 顾名思义 就是获取参数的
PathVariable 汉语意思是:路径变量,顾名思义,就是要获取一个url 地址中的一部分值,那一部分呢? RequestMapping 上说明了@RequestMapping(value="/emp/{id}"),我就是想获取你URL地址 /emp/ 的后面的那个 {id}的。
so,就看‘?’ 若是想获取 ‘?’ 后面的pageNo 的值 ‘2’, 就使用RequestParam ,
若想获取的是url 地址的一部分 ‘7’ 就使用PathVariable
@PathVariable is used to obtain dynamic parameters in the request url
The theory can be found in the following blog post
http://blog.csdn.net/walkerjong/article/details/7946109
Detailed explanation of parameter binding annotations such as @RequestParam @RequestBody @PathVariable
http://dorole.com/tag/uri-template/
http://blog.csdn.net/jaryle/article/details/51851120 The difference between @pathvariable and @RequestParam annotations