天天看点

@RequestMapping注解(超详细基础知识+实际代码案例)

1、@RequestMapping注解的功能

从注解名称上我们可以看到,

@RequestMapping

注解的作用就是将请求和处理请求的控制器方法关联起来,建立映射关系。

SpringMVC 接收到指定的请求,就会来找到在映射关系中对应的控制器方法来处理这个请求。

2、@RequestMapping注解的位置

  • @RequestMapping标识一个类:设置映射请求的请求路径的==初始信息==
  • @RequestMapping标识一个方法:设置映射请求请求路径的==具体信息==

2.1 作用在类上实例

比如A类和B类中都有一个list。为了区分是:在类上加上注解、那么访问的地址就变成

/A/list

/B/list

。很实用的一个功能

@Controller
@RequestMapping("/hello")
public class TestController {

    /**
     * 此时请求的路径是/hello/test
     * @return
     */
    @RequestMapping("/test")
    public String success(){
        return "success";
    }
}

           

访问方式

<a rel="nofollow" th:href="@{/hello/test}">访问目标页面target.html</a>
           

测试结果

@RequestMapping注解(超详细基础知识+实际代码案例)

2.2 仅作用在方法上

直接通过方法上的路径就可以访问到,

@Controller
public class TestController {

    /**
     * 此时请求的路径是test
     * @return
     */
    @RequestMapping("/test")
    public String success(){
        return "success";
    }
}

           

访问方式

<a rel="nofollow" th:href="@{/test}">访问目标页面target.html</a>
           

测试结果

@RequestMapping注解(超详细基础知识+实际代码案例)

3、@RequestMapping注解的value属性

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

注:value一般写一个匹配路径就行

3.1 测试

@Controller
public class TestController {
    @RequestMapping(value = {"/test","/myTest"})
    public String success(){
        return "success";
    }
}

           
<a rel="nofollow" th:href="@{/test}">访问目标页面target.html</a>

<a rel="nofollow" th:href="@{/myTest}">访问目标页面target.html</a>
           
@RequestMapping注解(超详细基础知识+实际代码案例)
@RequestMapping注解(超详细基础知识+实际代码案例)

4、@RequestMapping注解的method属性

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

    405:Request method 'POST' not supported

注:restful风格经常使用method和value联合使用

4.1、同时满足value和method

<form th:action="@{/test}" method="post">
    <input type="submit">
</form>
           
@Controller
public class TestController {
    @RequestMapping(
            value = {"/test", "/myTest"},
            method = {RequestMethod.GET, RequestMethod.POST}
    )
    public String success() {
        return "success";
    }
}
           
@RequestMapping注解(超详细基础知识+实际代码案例)

4.2 满足value、不满足method的情况

<form th:action="@{/test}" method="post">
    <input type="submit">
</form>
           
@Controller
public class TestController {
    @RequestMapping(value = {"/test","/myTest"},method = RequestMethod.GET)
    public String success(){
        return "success";
    }
}
           
@RequestMapping注解(超详细基础知识+实际代码案例)
1、对于处理指定请求方式的控制器方法,SpringMVC中提供

@RequestMapping

的派生注解

处理get请求的映射-->@GetMapping

处理post请求的映射-->@PostMapping

处理put请求的映射-->@PutMapping

处理delete请求的映射-->@DeleteMapping

2、常用的请求方式有get,post,put,delete

但是目前浏览器只支持get和post,若在form表单提交时,为method设置了其他请求方式的字符

串(put或delete),则按照默认的请求方式get处理

还是建议使用restful风格

@RequestMapping注解(超详细基础知识+实际代码案例)

5、@RequestMapping注解的params属性

  • @RequestMapping注解的params属性通过请求的请求参数匹配请求映射
  • @RequestMapping注解的params属性是一个字符串类型的数组,可以通过四种表达式设置请求参数和请求映射的匹配关系

1、"param":要求请求映射所匹配的请求必须携带param请求参数 "2、!param":要求请求映射所匹配的请求必须不能携带param请求参数

3、 "param=value":要求请求映射所匹配的请求必须携带param请求参数且param=value

4、"param!=value":要求请求映射所匹配的请求必须携带param请求参数但是param!=value

注:用的较多也是携带参数

5.1 使用数组的形式

<form th:action="@{/test(username='张三',password='1234')}" method="post">
    <input type="submit">
</form>
           
@Controller
public class TestController {
    @RequestMapping(
            value = {"/test", "/myTest"},
            method = {RequestMethod.GET, RequestMethod.POST},
            params = {"username","password"}
    )
    public String success() {
        return "success";
    }
}
           
@RequestMapping注解(超详细基础知识+实际代码案例)

5.2 不适用数组的形式

<form th:action="@{/test(username='张三')}" method="post">
    <input type="submit">
</form>
           
@Controller
public class TestController {
    @RequestMapping(
            value = {"/test", "/myTest"},
            method = {RequestMethod.GET, RequestMethod.POST},
            params = "username"
    )
    public String success() {
        return "success";
    }
}
           
@RequestMapping注解(超详细基础知识+实际代码案例)

5.3 参数不等于某个数的情况

<form th:action="@{/test(username='张三',password='1234')}" method="post">
    <input type="submit">
</form>
           
@Controller
public class TestController {
    @RequestMapping(
            value = {"/test", "/myTest"},
            method = {RequestMethod.GET, RequestMethod.POST},
            params = {"username","password!=1234"}
    )
    public String success() {
        return "success";
    }
}
           
若当前请求满足@RequestMapping注解的value和method属性,但是不满足params属性,此时页面回报错400
@RequestMapping注解(超详细基础知识+实际代码案例)
@RequestMapping注解(超详细基础知识+实际代码案例)

提示:这种情况目前我是没遇到过这样的写法

6、@RequestMapping注解的headers属性

  • @RequestMapping注解的headers属性通过请求的请求头信息匹配请求映射
  • @RequestMapping注解的headers属性是一个字符串类型的数组,可以通过四种表达式设置请求头信息和请求映射的匹配关系

"header":要求请求映射所匹配的请求必须携带header请求头信息

"!header":要求请求映射所匹配的请求必须不能携带header请求头信息

"header=value":要求请求映射所匹配的请求必须携带header请求头信息且header=value

"header!=value":要求请求映射所匹配的请求必须携带header请求头信息且header!=value

若当前请求满足@RequestMapping注解的value和method属性,但是不满足headers属性,此时页面显示404错误,即资源未找到

了解就行

6.1 测试

请求头中需要携带的信息

<form th:action="@{/test(username='张三',password='12345')}" method="post">
    <input type="submit">
</form>
           
@Controller
public class TestController {
    @RequestMapping(
            value = {"/test", "/myTest"},
            method = {RequestMethod.GET, RequestMethod.POST},
            params = {"username","password!=1234"},
            headers = {"Host=localhost:8088"}
    )
    public String success() {
        return "success";
    }
}
           
@RequestMapping注解(超详细基础知识+实际代码案例)

7、SpringMVC支持ant风格的路径

:表示任意的单个字符

*

:表示任意的0个或多个字符

**

:表示任意的一层或多层目录

注意:在使用**时,只能使用

/**/xxx

的方式

7.1 测试单个字符

<a rel="nofollow" th:href="@{/fgf/mytest}">访问目标页面target.html</a>
           
@Controller
public class TestController {
    @RequestMapping("/f?f/mytest")
    public String myTest() {
        return "success";
    }
    }
           
@RequestMapping注解(超详细基础知识+实际代码案例)

7.2测试多个字符

<a rel="nofollow" th:href="@{/fg54edrf/mytest}">访问目标页面target.html</a>

           
@Controller
public class TestController {
    @RequestMapping("/f*f/mytest")
    public String myTest() {
        return "success";
    }
    }
           
@RequestMapping注解(超详细基础知识+实际代码案例)

7.3 测试目录结构

多级目录都可以访问到、一般也不这样用吧

<a rel="nofollow" th:href="@{/fg54edrf/mytest}">访问目标页面target.html</a>

           
@Controller
public class TestController {
    @RequestMapping("/**/mytest")
    public String myTest() {
        return "success";
    }
    }
           
@RequestMapping注解(超详细基础知识+实际代码案例)

8、SpringMVC支持路径中的占位符(重点)

SpringMVC路径中的占位符常用于RESTful风格中、我就喜欢这样使用

原始方式:/deleteUser?id=1

rest方式:/deleteUser/1

SpringMVC路径中的占位符常用于RESTful风格中,当请求路径中将某些数据通过路径的方式传输到服务器中,就可以在相应的

@RequestMapping

注解的value属性中通过占位符{xxx}表示传输的数据,在通过

@PathVariable

注解,将占位符所表示的数据赋值给控制器方法的形参

8.1 测试

<a rel="nofollow" th:href="@{/mytest/4/张三}">访问目标页面target.html</a>

           
@Controller
public class TestController {
    @RequestMapping("/mytest/{id}/{username}")
    public String myTest(@PathVariable("id") String id,@PathVariable("username") String username) {
        System.out.println("id:"+ id+",username:"+username);
        return "success";
    }
           
@RequestMapping注解(超详细基础知识+实际代码案例)

8.2 项目中的实际引用

9、后语