SpringMVC之restful风格
- restful风格使请求地址栏中不再显示参数名, 只显示参数值, 提高安全性
- 请求路径中的参数使用
{key}
表示, key表示参数名- 使用
@PathVariable
注解绑定请求参数与方法参数
@GetMapping("/rest/{num1}/{num2}")
public String hello(@PathVariable("num2") int a, @PathVariable("num1") int b, Model model){
model.addAttribute("ans",a + b);
model.addAttribute("a",a);
model.addAttribute("b",b);
return "hello";
}
上述代码中请求路径中包含两个参数, num1 和 num2, 使用@PathVariable
注解将num1与方法参数b绑定, num2与方法参数a绑定