参考:https://www.kuangstudy.com/bbs/1422050997429198850
以前:
web输入: /add?a=1&b=5
package com.kuang.controller; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class testController { @RequestMapping("/add") public String test1(int a, int b , Model model){ int res=a+b; model.addAttribute("msg","结果为"+res); return "hello"; } }
现在:
http://localhost:8080/add/8/9
package com.kuang.controller; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class testController { @RequestMapping("/add/{a}/{b}") public String test1(@PathVariable int a, @PathVariable int b , Model model){ int res=a+b; model.addAttribute("msg","结果为"+res); return "hello"; } }
限定方法:
@RequestMapping(value="/add/{a}/{b}",method = RequestMethod.GET)
和
@GetMapping("/add/{a}/{b}")一样,限定方法,其他方法也是同样的道理