SpringMVC:@RequestMapping注解-1

本篇内容:@RequestMapping的功能,位置,属性(value,method)

1.@RequestMapping注解的功能

从注解名称上我们可以看到,@RequestMapping注解的作用就是将请求和处理请求的控制器方法关联
起来,建立映射关系。
SpringMVC 接收到指定的请求,就会来找到在映射关系中对应的控制器方法来处理这个请求。

2.@RequestMapping注解的位置
@RequestMapping标识一个类:设置映射请求的请求路径的初始信息
@RequestMapping标识一个方法:设置映射请求请求路径的具体信息

什么意思呢?举例说明:

如下在这个控制器类中,类和方法中都有@ResquestMapping注解

    @Controller
    @RequestMapping("/test")
    public class RequestMappingController {
        //此时请求映射所映射的请求的请求路径为:/test/testRequestMapping
        @RequestMapping("/testRequestMapping")
        public String testRequestMapping(){
            return "success";
        }
    }

那么在前端超链接中

<a th:href="@{/testRequestMapping}">跳转页面</a>

将无法访问,因为在类上面有注解@RequestMapping("/test"),所有要这样:

<a th:href="@{/test/testRequestMapping}">跳转页面</a>

3.@RequestMapping注解的value属性

@RequestMapping注解的value属性通过请求的请求地址匹配请求映射
@RequestMapping注解的value属性是一个字符串类型的数组,表示该请求映射能够匹配多个请求地址所对应的请求
@RequestMapping注解的value属性必须设置,至少通过请求地址匹配请求映射

    @RequestMapping(
            value = {"/testRequestMapping", "/test"}
    )
    public String testRequestMapping(){
        return "success";
    }

以下两种形式都可以进行访问

<a th:href="@{/testRequestMapping}">测试@RequestMapping的value属性-->/testRequestMapping</a><br>
<a th:href="@{/test}">测试@RequestMapping的value属性-->/test</a><br>

4.@RequestMapping注解的method属性

@RequestMapping注解的method属性通过请求的请求方式(get或post)匹配请求映射
@RequestMapping注解的method属性是一个RequestMethod类型的数组,表示该请求映射能够匹配多种请求方式的请求
若当前请求的请求地址满足请求映射的value属性,但是请求方式不满足method属性,则浏览器报错
405:Request method 'POST' not supported

    @RequestMapping(
            value = {"/testRequestMapping", "/test"},
            method = {RequestMethod.GET, RequestMethod.POST}
    )
    public String testRequestMapping(){
        return "success";
    }
<a th:href="@{/test}">测试@RequestMapping的value属性-->/test</a<br>
<form th:action="@{/test}" method="post">
         <input type="submit">
</form>

@RequestMapping中method什么也不写,那么post和get请求都可以,method的值与请求的方式不对就报错

405:Request method 'POST' not supported

注:
1、对于处理指定请求方式的控制器方法,SpringMVC中提供了@RequestMapping的派生注解
处理get请求的映射-->@GetMapping
处理post请求的映射-->@PostMapping
处理put请求的映射-->@PutMapping
处理delete请求的映射-->@DeleteMapping
2、常用的请求方式有get,post,put,delete

但是目前浏览器只支持get和post,若在form表单提交时,为method设置了其他请求方式的字符串(put或delete),则按照默认的请求方式get处理
若要发送put和delete请求,则需要通过spring提供的过滤器HiddenHttpMethodFilter,在RESTful部分会讲到

 

阅读剩余
THE END