Restful风格参数
通常使用get方式传递参数时,用的是?后面接参数名和参数值,而Restful可以简化参数格式。
前端发送
get方式
<a href="Test?id=001&name=zhangsan">传值</a>
Restful风格
<a href="Test/001/zhangsan">走你</a>
后端接收
get方式
@RequestMapping("Test")
public String Test(int id,String name){
System.out.println(id+name);
return "test";
}
Restful风格(使用@PathVariable)
@RequestMapping("Test/{id}/{username}")
public String Test(@PathVariable int id,@PathVariable("username") String name){
System.out.println(id+name);
return "test";
}
@RequestMapping 中的格式必须和前端请求发送的数据格式一致,至于{}中的键名则自定义即可,@PathVariable 是获取 @RequestMapping 中的内容,如果你在 @PathVariable 后面的()不指定键值,那么 @PathVariable 默认到 @RequestMapping 中匹配。