@RequestMapping的作用:就是将请求(request)和处理请求的控制器方法(控制层)关联起来,建立一个映射关系,SpringMVC接收到指定的请求,就会找到在映射关系中对应的控制器方法(控制层)来处理这个请求。
@RequestMapping在类上加:设置映射请求的请求路径的初始信息
@RequestMapping在方法上加:设置映射请求请求路径的具体信息
- @Controller
- @RequestMapping("/test")
- public class RequestMappingController {
- //此时请求映射所映射的请求的请求路径为:/test/testRequestMapping
- @RequestMapping("/testRequestMapping")
- public String testRequestMapping(){
- return "success";
- }
- }
即当前浏览器所发送请求的请求路径匹配value属性中的任何一个值,则当前请求就会被注解所标识的方法进行处理
- <a th:href="@{/testRequestMapping}">测试@RequestMapping的value属性--
- >/testRequestMappinga><br>
- <a th:href="@{/test}">测试@RequestMapping的value属性-->/testa><br>
- @RequestMapping(
- value = {"/testRequestMapping", "/test"}
- )
- public String testRequestMapping(){
- return "success";
- }
请求方法包括:get、post、head、option 、put、 patch、 delete、 trance
常用的请求方法包括:get、post、put、delete
注意:若要发送put或delete请求,则需要通过spring提供的过滤器HiddenHttpMethodFilter
表单和Ajax提交一般使用post,其他大部分都是get
- <form th:action="@{/hello}" method="post"><br>
- <input type="submit" value="测试@RequestMapping注解的method属性"><br>
- form>
- @RequestMapping(
- value = {"/testRequestMapping", "/test"},
- method = {RequestMethod.GET, RequestMethod.POST}
- )
- public String testRequestMapping(){
- return "success";
- }