@PathVariable : 从路径里获取变量
- 用于讲请求URL中的模板变量映射到功能处理方法的参数上
- 可以取出url中的变量作为方法的参数
// 请求的url:localhost:8080/checkUser/412002
@RequestMapping("checkUser/{userId}")
@ResponseBody
// 这里用@PathVariable就可以把412002这个在url里的userId给取出来了
public String check(@PathVariable("userId") String userId){
if(userId == null){
return "Empty User!";
}else{
return "findUser: "+userId;
}
}
@RequestParam: 从请求里获取参数
- 用于在SpringMVC后台控制(Service)层获取参数
// 请求的url:localhost:8080/checkUser/userId=412002
@RequestMapping("checkUser")
@ResponseBody
//z这里能代表userId参数是非必须
public String check(@RequestParam(value="userId",required=false) String userId){
if(userId == null){
return "Empty User!";
}else{
return "findUser: "+userId;
}
}