@RequestMapping注解

本篇示例基于第一个SpringMVC程序

@RequestMapping的定义
//RequestMapping.class

package org.springframework.web.bind.annotation;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.core.annotation.AliasFor;

@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Mapping
public @interface RequestMapping {
    String name() default "";

    @AliasFor("path")
    String[] value() default {};

    @AliasFor("value")
    String[] path() default {};

    RequestMethod[] method() default {};

    String[] params() default {};

    String[] headers() default {};

    String[] consumes() default {};

    String[] produces() default {};
}

@RequestMapping注解的作用

@RequestMapping的作用是,将请求、处理请求的控制器方法关联起来,建立映射关系。
在这里插入图片描述

@RequestMapping注解的位置
@Target({ElementType.TYPE, ElementType.METHOD})

@RequestMapping注解既可以标识在类上(ElementType.TYPE),也可以标识在方法(ElementType.METHOD)。比如,

@Controller
@RequestMapping("/test")
public class HelloController {
    @RequestMapping("/hello")
    public String testHello(){
        return "hello";
    }
}
<!-- hello.html -->
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8" >
    <title>Hello</title>
</head>
<body>
<h1>Here is hello page</h1>
</body>
</html>

此时,控制器方法testHello()映射的请求路径为/test/hello
在这里插入图片描述

@RequestMapping的属性

在这里插入图片描述

RequestMapping有如下属性:

  • name():String
  • value():String[]
  • path():String[]
  • method():RequestMethod[]
  • params():String[]
  • headers():String[]
  • consumers():String[]
  • produces():String[]
value属性

@RequestMapping(value=xxx),通过请求地址匹配请求。
value():String[],即 value属性值既可以是一个字符串,也可以是一个字符串数组。

@Controller
public class TestController {
    @RequestMapping(value = {"/test","/test/value"})
    public String testValue(){
        return "success";
    }
}
<!-- success.html -->
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8" >
    <title>success</title>
</head>
<body>
<h1>请求成功!</h1>
</body>
</html>

在这里插入图片描述
示例中,@RequestMapping(value = {"/test","/test/value"}),value属性值是一个字符串数组,表示同时匹配多个请求地址:/test/test/value
@RequestMapping(value = {"/test","/test/value"})@RequestMapping({"/test","/test/value"})

另外,关于请求地址的匹配,以下两点需要注意。
第一点:ant风格中的通配符
ant风格,是请求地址的一种匹配方式,用于模糊匹配,有如下3种通配符,

  • ?,用于匹配单个字符。比如@RequestMapping("/a?a/test"),可匹配/aaa/test/aba/test/a1a/test等请求地址。
  • *,用于匹配零个或多个字符。比如@RequestMapping("/a*a/test"),可匹配/aa/test/aca/test/abcdefa/test等请求地址。
  • **,用于匹配一层或多层路径,用于@RequestMapping("/**/xxxx")方式。比如/**/test,可匹配/test/user/test/a/b/c/d/test等请求地址。

第二点:rest风格中的占位符
请求路径上的占位符{xxx},常见于restful风格中。
把要传输给服务器的数据放到请求路径的占位符上,如@RequestMapping("/test/{id}")
使用@PathVariable注解,可将占位符表示的数据赋值给控制器方法的形参。

package com.example.mvc.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class TestController {
    @RequestMapping("/")
    public String index(){
        return "index";
    }

    @RequestMapping("/test/{id}")
    public String test(@PathVariable("id") Long id){
        System.out.println("id:"+id);
        return "success";
    }
}

在这里插入图片描述

method属性

@RequestMapping(method=xxx),通过请求方法匹配请求。
method():RequestMethod[],method属性值既可以是一个RequestMethod类型的方法,也可以是RequestMethod类型的数组。

package com.example.mvc.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class TestController {
    @RequestMapping("/")
    public String index(){
        return "index";
    }

    @RequestMapping(value = "/test",method = RequestMethod.GET)
    public String test(){
        return "success";
    }

    @RequestMapping(value = "/target",method = {RequestMethod.GET,RequestMethod.POST})
    public String target(){
        return "target";
    }
}
<!-- index.html -->
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>首页</title>
</head>
<body>
  <h1>Hello World!</h1>
  <form th:action="@{/test}" method="get">
      <input type="submit" value="method属性是一个字符串,method为get,访问success页面"/>
  </form>
  <form th:action="@{/target}" method="post">
      <input type="submit" value="method属性是一个字符串组,method为get,访问target页面"/>
  </form>
  <form th:action="@{/target}" method="post">
      <input type="submit" value="method属性是一个字符串组,method为post,访问tareget页面"/>
  </form>
</body>
</html>
<!-- success.html -->
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8" >
    <title>success</title>
</head>
<body>
<h1>here is success</h1>
</body>
</html>
<!-- target.html -->
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>target</title>
</head>
<body>
  <h1>here is target</h1>
</body>
</html>

示例中,@RequestMapping(value = "/test",method = RequestMethod.GET),请求路径匹配/test,请求方法匹配get方法;
@RequestMapping(value = "/target",method = {RequestMethod.GET,RequestMethod.POST}),请求路径匹配/target,可匹配多个请求方法:get和post。

常用的请求方法有:get、post、put和delete,针对这些方法,Spring提供了@RequestMapping对应的派生注解,如下,

  • @GetMapping,同@RequestMapping(method = RequestMethod.GET)
  • @PostMapping,同@RequestMapping(method = RequestMethod.POST)
  • @PutMapping,同@RequestMapping(method = RequestMethod.PUT)
  • @DeleteMapping,同@RequestMapping(method = RequestMethod.DELETE)
params属性

@RequestMapping(params=xxx),通过请求参数匹配请求。

package com.example.mvc.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@Controller
public class TestController {
    @RequestMapping("/")
    public String index(){
        return "index";
    }

    @GetMapping(value = "/test",params = {"username"})
    public String test(){
        return "success";
    }
    @GetMapping(value = "/test2",params = {"!username"})
    public String test2(){
        return "success";
    }
    @GetMapping(value = "/test3",params = {"username=tom"})
    public String test3(){
        return "success";
    }
    @GetMapping(value = "/test4",params = {"username!=tom"})
    public String test4(){
        return "success";
    }
}
<!-- index.html -->
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>首页</title>
</head>
<body>
  <h1>Hello World!</h1>
  <a th:href="@{/test(username='tom')}">@{/test(username='tom')}<-->@GetMapping(value = "/test",params = {"username"}),测试:Yes</a><br/>
  <a th:href="@{/test}">@{/test}<-->@GetMapping(value = "/test",params = {"username"}),测试:No</a><br/><br/>

  <a th:href="@{/test2}">@{/test2}<--->@GetMapping(value = "/test2",params = {"!username"}),测试2:Yes</a><br/>
  <a th:href="@{/test2(username='tom')}">@{/test2(username='tom')}<--->@GetMapping(value = "/test2",params = {"!username"}),测试2:No</a><br/><br/>

  <a th:href="@{/test3(username='tom')}">@{/test3(username='tom')}<--->@GetMapping(value = "/test3",params = {"username=tom"}),测试3:Yes</a><br/>
  <a th:href="@{/test3(username='jack')}">@{/test3(username='jack')}<--->@GetMapping(value = "/test3",params = {"username=tom"}),测试3:No</a><br/><br/>

  <a th:href="@{/test4(username='jack')}">@{/test4(username='jack')}<--->@GetMapping(value = "/test4",params = {"username!=tom"}),测试4:Yes</a><br/>
  <a th:href="@{/test4(username='tom')}">@{/test4(username='tom')}<--->@GetMapping(value = "/test4",params = {"username!=tom"}),测试4:No</a>
</body>
</html>
<!-- success.html -->
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8" >
    <title>success</title>
</head>
<body>
<h1>here is success</h1>
</body>
</html>

在这里插入图片描述
在这里插入图片描述

  • params = {"username"},请求参数必须携带username。
  • params = {"!username"},请求参数不能携带username。
  • params = {"username=tom"},请求参数必须携带username,且username的值是tom。
  • params = {"username!=tom"},请求参数必须携带username,且username的值不能是tom。

如果需要匹配多个请求参数,将params设置为字符串数组即可,如params = {"username","password"}

package com.example.mvc.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@Controller
public class TestController {
    @RequestMapping("/")
    public String index(){
        return "index";
    }

    @GetMapping(value = "/test",params = {"username","password"})
    public String test(){
        return "success";
    }
}
<!-- index.html -->
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>首页</title>
</head>
<body>
  <h1>Hello World!</h1>
  <a th:href="@{/test(username='tom',password=123456)}">测试</a>
</body>
</html>

在这里插入图片描述

headers属性

@RequestMapping(headers=xxx),通过请求头匹配请求。

package com.example.mvc.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@Controller
public class TestController {
    @RequestMapping("/")
    public String index(){
        return "index";
    }

    @GetMapping(value = "/test",headers = {"Host"})
    public String test(){
        return "success";
    }
    @GetMapping(value = "/test2",headers = {"!Host"})
    public String test2(){
        return "success";
    }
    @GetMapping(value = "/test3",headers = {"Host=localhost:8080"})
    public String test3(){
        return "success";
    }
    @GetMapping(value = "/test4",headers = {"Host!=localhost:8080"})
    public String test4(){
        return "success";
    }

}
<!-- index.html -->
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>首页</title>
</head>
<body>
  <h1>Hello World!</h1>
  <a th:href="@{/test}">测试</a>
  <a th:href="@{/test2}">测试2</a>
  <a th:href="@{/test3}">测试3</a>
  <a th:href="@{/test4}">测试4</a>
</body>
</html>
<!-- success.html -->
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8" >
    <title>success</title>
</head>
<body>
<h1>here is success</h1>
</body>
</html>

在这里插入图片描述

  • headers = {"Host"},表示请求头信息必须携带Host。
  • headers = {"!Host"},表示请求头信息不能携带Host。
  • headers = {"Host=localhost:8080"},表示请求头信息必须携带Host,且Host的值必须为localhost:8080。
  • headers = {"Host!=localhost:8080"},表示请求头信息必须携带Host,且Host的值不能是localhost:8080。
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值