- 正常url访问:
http://localhost:8080/springMVC_04_controller_war_exploded/test1?a=1&b=2
@RequestMapping("/test1")
public String test1(int a,int b, Model model){
int num = a+b;
model.addAttribute("msg","结果为"+num);
return "test";
}
2./分隔方式:http://localhost:8080/springMVC_04_controller_war_exploded/test1/9/2
@RequestMapping("/test1/{a}/{b}")
public String test1(@PathVariable int a,@PathVariable int b, Model model){
int num = a+b;
model.addAttribute("msg","结果为"+num);
return "test";
}
- 指定请求方式访问
@RequestMapping(path="",method= RequestMethod."")
@RequestMapping(path="/test1/{a}/{b}",method= RequestMethod.GET)
public String test1(@PathVariable int a,@PathVariable int b, Model model){
int num = a+b;
model.addAttribute("msg","结果为"+num);
return "test";
}
RequestMethod的枚举如下:
- 用对应的注解指定访问方式(GetMapping、PostMapping、DeleteMapping、PutMapping…)
@GetMapping("/test1/{a}/{b}")
public String test1(@PathVariable int a,@PathVariable int b, Model model){
int num = a+b;
model.addAttribute("msg","结果为"+num);
return "test";
}