首页 > 其他分享 >SpringMVC03 - @RequestMapping

SpringMVC03 - @RequestMapping

时间:2023-02-15 21:57:22浏览次数:44  
标签:SpringMVC03 匹配 RequestMapping 路径 value 注解 请求

@RequestMapping

1. @RequestMapping 标识的位置

  • @RequestMapping标识一个类:设置映射请求的请求路径的初始信息
  • @RequestMapping标识一个方法:设置映射请求请求路径的具体信息
  • 作用在类上,表示类中的所有方法都必须有类注解的路径

  • @Controller
    @RequestMapping("/class")
    public class TestRequestMappingController {
    
        @RequestMapping("/method")
        public String test(){
    
            return "index";
        }
    }
    
  • 类中所有方法处理的请求路径 根路径/class/方法路径

  • test方法处理的请求路径为 根路径/class/method

2. @RequestMapping 注解的 value属性

  • 作用:通过请求的请求路径匹配请求
  • value属性是数组类型,即当前浏览器所发送请求的请求路径匹配value属性中的任何一个值
  • 则当前请求就会被注解所标识的方法进行处理
  • @Controller
    @RequestMapping("/class")
    public class TestRequestMappingController {
    
        //@RequestMapping(value = {"/hello","/abc"})
        @RequestMapping({"/hello","/abc"})
        public String hello(){
    
            return "success";
        }
    }
    
  • 只有 value一个属性时,value可以省略

3.@RequestMapping 注解的 method属性

  • 作用:通过请求的请求方式匹配请求
  • method属性是RequestMethod类型的数组,即当前浏览器所发送请求的请求方式匹配method属性中的任何一中请求方式,则当前请求就会被注解所标识的方法进行处理
  • 若浏览器所发送的请求的请求路径和@RequestMapping注解value属性匹配,但是请求方式不匹配,此时页面报错:405 - Request method 'xxx' not supported
  • 在@RequestMapping的基础上,结合请求方式的一些派生注解:
    • @GetMapping,@PostMapping,@DeleteMapping,@PutMapping
  • @Controller
    @RequestMapping("/class")
    public class TestRequestMappingController {
    
        @RequestMapping(
                value = {"/hello","/abc"},
                method = { RequestMethod.GET, RequestMethod.POST } )
        public String hello(){
    
            return "success";
        }
    }
    
  • 目前学过的基础请求中,除了表单提交,其余请求都是 GET 请求

4. @RequestMapping 注解的 params属性(了解,一般不使用)

  • 作用:通过请求的请求参数匹配请求,即浏览器发送的请求的请求参数必须满足params属性的设置
  • params可以使用四种表达式:
    • "param":表示当前所匹配请求的请求参数中必须携带param参数
    • "!param":表示当前所匹配请求的请求参数中一定不能携带param参数
    • "param=value":表示当前所匹配请求的请求参数中必须携带param参数且值必须为value
    • "param!=value":表示当前所匹配请求的请求参数中可以不携带param,若携带值一定不能是value
  • 若浏览器所发送的请求的请求路径和@RequestMapping注解value属性匹配,但是请求参数不匹配
    • 此时页面报错:400 - Parameter conditions "username" not met for actual request parameters:
@RequestMapping(
    value = {"/hello","/abc"},
    method = {RequestMethod.POST, RequestMethod.GET},
    params = {"username","!password","age=20","gender!=女"}
)
public String hello(){
    return "success";
}

5. @RequestMapping 注解的 headers属性(了解,一般不使用)

  • 作用:通过请求的请求头信息匹配请求,即浏览器发送的请求的请求头信息必须满足headers属性的设置

  • 若浏览器所发送的请求的请求路径和@RequestMapping注解value属性匹配,但是请求头信息不匹配

    • 此时页面报错:404
  • @RequestMapping(
        value = {"/hello","/abc"},
        method = {RequestMethod.POST, RequestMethod.GET},
        params = {"username","!password","age=20","gender!=女"},
        headers = {"referer"}
    )
    public String hello(){
        return "success";
    }
    
  • referer 表示跳转到当前页面的上一个页面地址

6. SpringMVC 支持 ant风格的路径

  • 在@RequestMapping注解的value属性值中设置一些特殊字符
    • ? :表示任意的单个字符( 不包括 ? )
    • * :表示任意的0个或多个字符( 不包括 ? 和 / )
    • ** :表示任意层数的任意目录,注意:在使**时,只能使用/**/xxx的方式,否则被视为两个单个的*
  • //http://localhost:8080/spring_mvc/aaa/bzb/czzc/asdasd
    @RequestMapping("/aaa/b?b/c**c/**")
    public String testant(){
    
        return "success";
    }
    

7. @RequestMapping 注解使用路径中的占位符 rest(重点,常用)

  • 传统:/deleteUser?id=1
  • rest:/user/delete/1
  • 需要在@RequestMapping注解的value属性中所设置的路径中,使用{xxx}的方式表示路径中的数据
  • 在通过@PathVariable注解,将占位符所标识的值和控制器方法的形参进行绑定
  • 请求路径 http://localhost:8080/spring_mvc/test/rest/admin/1

  • @RequestMapping("/test/rest/{username}/{id}")
    public String testRest(@PathVariable("id") Integer id, @PathVariable("username") String username){
        System.out.println("id:"+id+",username:"+username);
        return "success";
    }
    
  • 服务器处理结果:username = admin, id = 1

标签:SpringMVC03,匹配,RequestMapping,路径,value,注解,请求
From: https://www.cnblogs.com/Ashen-/p/17124841.html

相关文章