• (续)SSM整合之springmvc笔记(@RequestMapping注解)(P124-130)还没完


    目录

    @RequestMapping注解

    一.准备工作

    1 新建spring_mvc_demo    com.atguigu

     2. 导入依赖

    3 .添加web模板

     4 . 在web.xml里面进行进行配置

    5 .在resources下面创建配置文件springmvc.xml

    6 创建控制层

    7 . 创建入口控制层   用来访问首页

    8 . 创建首页

    9 . 部暑工程

    10 .启动tomcat

    11 .测试结果

    二  .@RequestMapping注解的功能

    三 .@RequestMapping注解的位置

    四 .  @RequestMapping注解的value属性 

    五 . @RequestMapping注解的method属性

    六  .@RequestMapping注解的params属性(了解)

    七   @RequestMapping注解的headers属性(了解)

    八   SpringMVC支持ant风格的路径

    1 .     ?:表示任意的单个字符

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

    3 .      **:表示任意层数的任意目录

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


    @RequestMapping注解

    一.准备工作

    1 新建spring_mvc_demo    com.atguigu

     2. 导入依赖

    war
    
    
        
        
            org.springframework
            spring-webmvc
            5.3.1
        
    
        
        
            ch.qos.logback
            logback-classic
            1.2.3
        
    
        
        
            javax.servlet
            javax.servlet-api
            3.1.0
            provided
        
    
        
        
            org.thymeleaf
            thymeleaf-spring5
            3.0.12.RELEASE
        
    
    

    3 .添加web模板

    第一种方式

    在spring_mvc_demo后面添加\src\main\webapp\

    整体D:\review_ssm\workspace\SSM\spring_mvc_demo\src\main\webapp\WEB-INF\web.xml

    第二种方式:

     4 . 在web.xml里面进行进行配置

    1. "1.0" encoding="UTF-8"?>
    2. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4. xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
    5. version="4.0">
    6. <servlet>
    7. <servlet-name>SpringMVCservlet-name>
    8. <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
    9. <init-param>
    10. <param-name>contextConfigLocationparam-name>
    11. <param-value>classpath:springmvc.xmlparam-value>
    12. init-param>
    13. <load-on-startup>1load-on-startup>
    14. servlet>
    15. <servlet-mapping>
    16. <servlet-name>SpringMVCservlet-name>
    17. <url-pattern>/url-pattern>
    18. servlet-mapping>
    19. web-app>

    5 .在resources下面创建配置文件springmvc.xml

    1. "1.0" encoding="UTF-8"?>
    2. <beans xmlns="http://www.springframework.org/schema/beans"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4. xmlns:context="http://www.springframework.org/schema/context"
    5. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
    6. <context:component-scan base-package="com.atguigu.controller">context:component-scan>
    7. <bean id="viewResolver" class="org.thymeleaf.spring5.view.ThymeleafViewResolver">
    8. <property name="order" value="1"/>
    9. <property name="characterEncoding" value="UTF-8"/>
    10. <property name="templateEngine">
    11. <bean class="org.thymeleaf.spring5.SpringTemplateEngine">
    12. <property name="templateResolver">
    13. <bean class="org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver">
    14. <property name="prefix" value="/WEB-INF/templates/"/>
    15. <property name="suffix" value=".html"/>
    16. <property name="templateMode" value="HTML5"/>
    17. <property name="characterEncoding" value="UTF-8" />
    18. bean>
    19. property>
    20. bean>
    21. property>
    22. bean>
    23. beans>

    6 创建控制层

    1. package com.atguigu.controller;
    2. import org.springframework.stereotype.Controller;
    3. import org.springframework.web.bind.annotation.PathVariable;
    4. import org.springframework.web.bind.annotation.RequestMapping;
    5. import org.springframework.web.bind.annotation.RequestMethod;
    6. @Controller
    7. public class TestRequestMappingController {
    8. }

     7 . 创建入口控制层   用来访问首页

    1. @Controller
    2. public class ProtalController {
    3. @RequestMapping("/") //访问首页
    4. public String protal(){
    5. return "index";
    6. }
    7. }

    8 . 创建首页

    1. html>
    2. <html lang="en" xmlns:th="http://www.thymeleaf.org">
    3. <head>
    4. <meta charset="UTF-8">
    5. <title>首页title>
    6. head>
    7. <body>
    8. <h1>index.htmlh1>
    9. body>
    10. html>

     9 . 部暑工程

     

     10 .启动tomcat

    11 .测试结果

    二  .@RequestMapping注解的功能

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

    三 .@RequestMapping注解的位置

    @RequestMapping 标识一个类:设置映射请求的请求路径的初始信息
    @RequestMapping 标识一个方法:设置映射请求请求路径的具体信息
    我们打开ProtalController类  然后按住Ctrl+左健 打开@RequestMapping注解
    解释:@Target({ElementType.TYPE, ElementType.METHOD})
    

    @Target:当前注解所能标识的位置 我们可以把他标记在一个类上TYPE 也可以把它标记在一个方法上METHOD

    现在我们演示把它标记在一个类

    第一步: 在TestRequestMappingController类写hello()方法

    1. /*
    2. * 1、@RequestMapping注解标识的位置
    3. @RequestMapping标识一个类:设置映射请求的请求路径的初始信息
    4. @RequestMapping标识一个方法:设置映射请求请求路径的具体信息
    5. * */
    6. @Controller
    7. @RequestMapping("/test") //设置当前映射的初始信息
    8. public class TestRequestMappingController {
    9. //此时控制器方法所匹配的请求的请求路径为/test/hello
    10. @RequestMapping("/hello") //设置映射的具体信息
    11. public String hello() {
    12. return "success";
    13. }
    14. }

    第二步:写success.html首页

    1. html>
    2. <html lang="en" xmlns:th="http://www.thymeleaf.org">
    3. <head>
    4. <meta charset="UTF-8">
    5. <title>首页title>
    6. head>
    7. <body>
    8. <h1>success.htmlh1>
    9. body>
    10. html>

    第三步:写index.html

    1. html>
    2. <html lang="en" xmlns:th="http://www.thymeleaf.org">
    3. <head>
    4. <meta charset="UTF-8">
    5. <title>首页title>
    6. head>
    7. <body>
    8. <h1>index.htmlh1>
    9. <a th:href="@{/hello}">测试@RequestMapping注解所标识的位置a><br>
    10. body>
    11. html>

    第四步:测试

     报404的原因是

    因为在类上加上了@RequestMapping("/test")之后  我们当前访问的请求路径应该是test下面的hello 才能匹配到控制器方法hello()

    解决办法如下

     怎么用

    2个控制层都可以对hello路径进行处理  这个时候就会报错 所以我们要在类上加上@RequestMapping("/test") 这时我们的控制器方法所匹配的请求的请求路径为/test/hello

    TestRequestMappingController

    ProtalController

    启动tomcat

    报错:

    Caused by: java.lang.IllegalStateException: Ambiguous mapping. Cannot map 'testRequestMappingController' method
    com.atguigu.controller.TestRequestMappingController#hello()
    to { [/hello]}: There is already 'protalController' bean method

    模糊不清的一个映射  不能去映射'testRequestMappingController'里面的hello()方法

    因为 'protalController里面已经有了一个方法进行映射了

    总结

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

    四 .  @RequestMapping注解的value属性 

    @RequestMapping 注解的 value 属性通过请求的请求地址匹配请求映射
    @RequestMapping 注解的 value 属性是一个字符串类型的数组,表示该请求映射能够匹配多个请求地址 所对应的请求
    @RequestMapping 注解的 value 属性必须设置,至少通过请求地址匹配请求映射
    TestRequestMappingController
    1. /*
    2. * 1、@RequestMapping注解标识的位置
    3. * @RequestMapping标识一个类:设置映射请求的请求路径的初始信息
    4. * @RequestMapping标识一个方法:设置映射请求请求路径的具体信息
    5. * 2、@RequestMapping注解value属性
    6. * 作用:通过请求的请求路径匹配请求
    7. * value属性是数组类型,即当前浏览器所发送请求的请求路径匹配value属性中的任何一个值
    8. * 则当前请求就会被注解所标识的方法进行处理
    9. * */
    10. @Controller
    11. //@RequestMapping("/test") //设置当前映射的初始信息
    12. public class TestRequestMappingController {
    13. //此时控制器方法所匹配的请求的请求路径为/test/hello
    14. @RequestMapping({"/hello","/abc"}) //设置映射的具体信息
    15. public String hello() {
    16. return "success";
    17. }
    18. }
    index.html
    1. html>
    2. <html lang="en" xmlns:th="http://www.thymeleaf.org">
    3. <head>
    4. <meta charset="UTF-8">
    5. <title>首页title>
    6. head>
    7. <body>
    8. <h1>index.htmlh1>
    9. <a th:href="@{/hello}">测试@RequestMapping注解所标识的位置a><br>
    10. <a th:href="@{/abc}">测试@RequestMapping注解的value属性a><br>
    11. body>
    12. html>

    测试

     总结:

     @RequestMapping注解value属性
    * 作用:通过请求的请求路径匹配请求
    * value属性是数组类型,即当前浏览器所发送请求的请求路径匹配value属性中的任何一个值
    * 则当前请求就会被注解所标识的方法进行处理

    五 . @RequestMapping注解的method属性

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

    /**
     * The HTTP request methods to map to, narrowing the primary mapping:
     * GET, POST, HEAD, OPTIONS, PUT, PATCH, DELETE, TRACE.
     * 

    Supported at the type level as well as at the method level! * When used at the type level, all method-level mappings inherit this * HTTP method restriction. */ RequestMethod[] method() default {};

     Crtl+左健  打开RequestMethod[]  枚举类型

    表单提交发出的是post或是axios里面把请求方式设置成post  其他的都是get,比如超链接来发送请求   地址栏直接输入地址来发送请求  都是get

    1. /*
    2. * 1、@RequestMapping注解标识的位置
    3. * @RequestMapping标识一个类:设置映射请求的请求路径的初始信息
    4. * @RequestMapping标识一个方法:设置映射请求请求路径的具体信息
    5. * 2、@RequestMapping注解value属性
    6. * 作用:通过请求的请求路径匹配请求
    7. * value属性是数组类型,即当前浏览器所发送请求的请求路径匹配value属性中的任何一个值
    8. * 则当前请求就会被注解所标识的方法进行处理
    9. * 3、@RequestMapping注解的method属性
    10. * 作用:通过请求的请求方式匹配请求
    11. * method属性是RequestMethod类型的数组,即当前浏览器所发送请求的请求方式匹配method属性中的任何一中请求方式
    12. * 则当前请求就会被注解所标识的方法进行处理
    13. * 若浏览器所发送的请求的请求路径和@RequestMapping注解value属性匹配,但是请求方式不匹配
    14. * 此时页面报错:405 - Request method 'xxx' not supported
    15. * 在@RequestMapping的基础上,结合请求方式的一些派生注解:
    16. * @GetMapping,@PostMapping,@DeleteMapping,@PutMapping
    17. * */
    18. @Controller
    19. //@RequestMapping("/test") //设置当前映射的初始信息
    20. public class TestRequestMappingController {
    21. //此时控制器方法所匹配的请求的请求路径为/test/hello
    22. @RequestMapping(
    23. value = {"/hello","/abc"},
    24. method = RequestMethod.GET
    25. )
    26. public String hello() {
    27. return "success";
    28. }
    29. }

    测试

     现在我们把请求方式设置成post

    1. package com.atguigu.controller;
    2. import org.springframework.stereotype.Controller;
    3. import org.springframework.web.bind.annotation.RequestMapping;
    4. import org.springframework.web.bind.annotation.RequestMethod;
    5. /*
    6. * 1、@RequestMapping注解标识的位置
    7. * @RequestMapping标识一个类:设置映射请求的请求路径的初始信息
    8. * @RequestMapping标识一个方法:设置映射请求请求路径的具体信息
    9. * 2、@RequestMapping注解value属性
    10. * 作用:通过请求的请求路径匹配请求
    11. * value属性是数组类型,即当前浏览器所发送请求的请求路径匹配value属性中的任何一个值
    12. * 则当前请求就会被注解所标识的方法进行处理
    13. * 3、@RequestMapping注解的method属性
    14. * 作用:通过请求的请求方式匹配请求
    15. * method属性是RequestMethod类型的数组,即当前浏览器所发送请求的请求方式匹配method属性中的任何一中请求方式
    16. * 则当前请求就会被注解所标识的方法进行处理
    17. * 若浏览器所发送的请求的请求路径和@RequestMapping注解value属性匹配,但是请求方式不匹配
    18. * 此时页面报错:405 - Request method 'xxx' not supported
    19. * 在@RequestMapping的基础上,结合请求方式的一些派生注解:
    20. * @GetMapping,@PostMapping,@DeleteMapping,@PutMapping
    21. * */
    22. @Controller
    23. //@RequestMapping("/test") //设置当前映射的初始信息
    24. public class TestRequestMappingController {
    25. //此时控制器方法所匹配的请求的请求路径为/test/hello
    26. @RequestMapping(
    27. value = {"/hello","/abc"},
    28. //method = RequestMethod.GET
    29. method = RequestMethod.POST
    30. )
    31. public String hello() {
    32. return "success";
    33. }
    34. }

    1. html>
    2. <html lang="en" xmlns:th="http://www.thymeleaf.org">
    3. <head>
    4. <meta charset="UTF-8">
    5. <title>首页title>
    6. head>
    7. <body>
    8. <h1>index.htmlh1>
    9. <a th:href="@{/hello}">测试@RequestMapping注解所标识的位置a><br>
    10. <a th:href="@{/abc}">测试@RequestMapping注解的value属性a><br>
    11. <form th:action="@{/hello}" method="post">
    12. <input type="submit" value="测试@RequestMapping注解的method属性">
    13. form>
    14. body>
    15. html>
    16. [点击并拖拽以移动]

     注意:表单在没有设置post请求时 默认的还是get请求   也就是说上面三种都是get请求

    而现在我们在控制层里面的@RequestMapping注解里面把请求方式设置层了post请求,

    若浏览器所发送的请求的请求路径和@RequestMapping注解value属性匹配,但是请求方式不匹配,此时页面报错:405 - Request method 'xxx' not supported

    测试

     

    现在我们把表单的请求方式改成post   重新部暑

    1. html>
    2. <html lang="en" xmlns:th="http://www.thymeleaf.org">
    3. <head>
    4. <meta charset="UTF-8">
    5. <title>首页title>
    6. head>
    7. <body>
    8. <h1>index.htmlh1>
    9. <a th:href="@{/hello}">测试@RequestMapping注解所标识的位置a><br>
    10. <a th:href="@{/abc}">测试@RequestMapping注解的value属性a><br>
    11. <form th:action="@{/hello}" method="post">
    12. <input type="submit" value="测试@RequestMapping注解的method属性">
    13. form>
    14. body>
    15. html>

     现在我们让他匹配get和post

    1. /*
    2. * 1、@RequestMapping注解标识的位置
    3. * @RequestMapping标识一个类:设置映射请求的请求路径的初始信息
    4. * @RequestMapping标识一个方法:设置映射请求请求路径的具体信息
    5. * 2、@RequestMapping注解value属性
    6. * 作用:通过请求的请求路径匹配请求
    7. * value属性是数组类型,即当前浏览器所发送请求的请求路径匹配value属性中的任何一个值
    8. * 则当前请求就会被注解所标识的方法进行处理
    9. * 3、@RequestMapping注解的method属性
    10. * 作用:通过请求的请求方式匹配请求
    11. * method属性是RequestMethod类型的数组,即当前浏览器所发送请求的请求方式匹配method属性中的任何一中请求方式
    12. * 则当前请求就会被注解所标识的方法进行处理
    13. * 若浏览器所发送的请求的请求路径和@RequestMapping注解value属性匹配,但是请求方式不匹配
    14. * 此时页面报错:405 - Request method 'xxx' not supported
    15. * 在@RequestMapping的基础上,结合请求方式的一些派生注解:
    16. * @GetMapping,@PostMapping,@DeleteMapping,@PutMapping
    17. * */
    18. @Controller
    19. //@RequestMapping("/test") //设置当前映射的初始信息
    20. public class TestRequestMappingController {
    21. //此时控制器方法所匹配的请求的请求路径为/test/hello
    22. @RequestMapping(
    23. value = {"/hello","/abc"},
    24. //method = RequestMethod.GET
    25. // method = RequestMethod.POST
    26. method = {RequestMethod.POST, RequestMethod.GET}
    27. )
    28. public String hello() {
    29. return "success";
    30. }
    31. }

     重新部暑

    派生注解:

    在@RequestMapping的基础上,结合请求方式的一些派生注解: * @GetMapping,@PostMapping,@DeleteMapping,@PutMapping

    注:
    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 部分会讲到

    总结:

    @RequestMapping注解的method属性
    * 作用:通过请求的请求方式匹配请求
    * method属性是RequestMethod类型的数组,即当前浏览器所发送请求的请求方式匹配method属性中的任何一中请求方式
    * 则当前请求就会被注解所标识的方法进行处理
    * 若浏览器所发送的请求的请求路径和@RequestMapping注解value属性匹配,但是请求方式不匹配
    * 此时页面报错:405 - Request method 'xxx' not supported
    * 在@RequestMapping的基础上,结合请求方式的一些派生注解:
    * @GetMapping,@PostMapping,@DeleteMapping,@PutMapping

    六  .@RequestMapping注解的params属性(了解)

    @RequestMapping 注解的 params 属性通过请求的请求参数匹配请求映射
    @RequestMapping 注解的 params 属性是一个字符串类型的数组,可以通过四种表达式设置请求参数和请求映射的匹配关系
    "param" :要求请求映射所匹配的请求必须携带 param 请求参数
    "!param" :要求请求映射所匹配的请求必须不能携带 param 请求参数
    "param=value" :要求请求映射所匹配的请求必须携带 param 请求参数且 param=value
    "param!=value" :要求请求映射所匹配的请求必须携带 param 请求参数但是 param!=value
    /**
     * The parameters of the mapped request, narrowing the primary mapping.
     * 

    Same format for any environment: a sequence of "myParam=myValue" style * expressions, with a request only mapped if each such parameter is found * to have the given value. Expressions can be negated by using the "!=" operator, * as in "myParam!=myValue". "myParam" style expressions are also supported, * with such parameters having to be present in the request (allowed to have * any value). Finally, "!myParam" style expressions indicate that the * specified parameter is not supposed to be present in the request. *

    Supported at the type level as well as at the method level! * When used at the type level, all method-level mappings inherit this * parameter restriction. */ String[] params() default {};

    第一种写法

    1. package com.atguigu.controller;
    2. import org.springframework.stereotype.Controller;
    3. import org.springframework.web.bind.annotation.RequestMapping;
    4. import org.springframework.web.bind.annotation.RequestMethod;
    5. /*
    6. * 1、@RequestMapping注解标识的位置
    7. * @RequestMapping标识一个类:设置映射请求的请求路径的初始信息
    8. * @RequestMapping标识一个方法:设置映射请求请求路径的具体信息
    9. * 2、@RequestMapping注解value属性
    10. * 作用:通过请求的请求路径匹配请求
    11. * value属性是数组类型,即当前浏览器所发送请求的请求路径匹配value属性中的任何一个值
    12. * 则当前请求就会被注解所标识的方法进行处理
    13. * 3、@RequestMapping注解的method属性
    14. * 作用:通过请求的请求方式匹配请求
    15. * method属性是RequestMethod类型的数组,即当前浏览器所发送请求的请求方式匹配method属性中的任何一中请求方式
    16. * 则当前请求就会被注解所标识的方法进行处理
    17. * 若浏览器所发送的请求的请求路径和@RequestMapping注解value属性匹配,但是请求方式不匹配
    18. * 此时页面报错:405 - Request method 'xxx' not supported
    19. * 在@RequestMapping的基础上,结合请求方式的一些派生注解:
    20. * @GetMapping,@PostMapping,@DeleteMapping,@PutMapping
    21. * 4、@RequestMapping注解的params属性
    22. * 作用:通过请求的请求参数匹配请求,即浏览器发送的请求的请求参数必须满足params属性的设置
    23. * params可以使用四种表达式:
    24. * "param":表示当前所匹配请求的请求参数中必须携带param参数
    25. * "!param":表示当前所匹配请求的请求参数中一定不能携带param参数
    26. * "param=value":表示当前所匹配请求的请求参数中必须携带param参数且值必须为value
    27. * "param!=value":表示当前所匹配请求的请求参数中可以不携带param,若携带值一定不能是value
    28. * 若浏览器所发送的请求的请求路径和@RequestMapping注解value属性匹配,但是请求参数不匹配
    29. * 此时页面报错:400 - Parameter conditions "username" not met for actual request parameters:
    30. *
    31. *
    32. *
    33. * */
    34. @Controller
    35. //@RequestMapping("/test") //设置当前映射的初始信息
    36. public class TestRequestMappingController {
    37. //此时控制器方法所匹配的请求的请求路径为/test/hello
    38. @RequestMapping(
    39. value = {"/hello","/abc"},
    40. //method = RequestMethod.GET
    41. // method = RequestMethod.POST
    42. method = {RequestMethod.POST, RequestMethod.GET},
    43. params = {"username"}
    44. )
    45. public String hello() {
    46. return "success";
    47. }
    48. }

     

    报400  当前参数的条件与真实的请求参数不匹配

    第一种方式

    测试@RequestMapping注解的params属性

     修改   index.html

     

    1. html>
    2. <html lang="en" xmlns:th="http://www.thymeleaf.org">
    3. <head>
    4. <meta charset="UTF-8">
    5. <title>首页title>
    6. head>
    7. <body>
    8. <h1>index.htmlh1>
    9. <a th:href="@{/hello}">测试@RequestMapping注解所标识的位置a><br>
    10. <a th:href="@{/abc}">测试@RequestMapping注解的value属性a><br>
    11. <form th:action="@{/hello}" method="post">
    12. <input type="submit" value="测试@RequestMapping注解的method属性">
    13. form>
    14. <a th:href="@{/hello?username=admin}">测试@RequestMapping注解的params属性a><br>
    15. body>
    16. html>

    第二种方式

    测试@RequestMapping注解的params属性

    1. html>
    2. <html lang="en" xmlns:th="http://www.thymeleaf.org">
    3. <head>
    4. <meta charset="UTF-8">
    5. <title>首页title>
    6. head>
    7. <body>
    8. <h1>index.htmlh1>
    9. <a th:href="@{/hello}">测试@RequestMapping注解所标识的位置a><br>
    10. <a th:href="@{/abc}">测试@RequestMapping注解的value属性a><br>
    11. <form th:action="@{/hello}" method="post">
    12. <input type="submit" value="测试@RequestMapping注解的method属性">
    13. form>
    14. <a th:href="@{/hello?username=admin}">测试@RequestMapping注解的params属性a><br>
    15. <a th:href="@{/hello(username='admin')}">测试@RequestMapping注解的params属性a><br>
    16. body>
    17. html>

     

    第二种写法

    1. package com.atguigu.controller;
    2. import org.springframework.stereotype.Controller;
    3. import org.springframework.web.bind.annotation.RequestMapping;
    4. import org.springframework.web.bind.annotation.RequestMethod;
    5. /*
    6. * 1、@RequestMapping注解标识的位置
    7. * @RequestMapping标识一个类:设置映射请求的请求路径的初始信息
    8. * @RequestMapping标识一个方法:设置映射请求请求路径的具体信息
    9. * 2、@RequestMapping注解value属性
    10. * 作用:通过请求的请求路径匹配请求
    11. * value属性是数组类型,即当前浏览器所发送请求的请求路径匹配value属性中的任何一个值
    12. * 则当前请求就会被注解所标识的方法进行处理
    13. * 3、@RequestMapping注解的method属性
    14. * 作用:通过请求的请求方式匹配请求
    15. * method属性是RequestMethod类型的数组,即当前浏览器所发送请求的请求方式匹配method属性中的任何一中请求方式
    16. * 则当前请求就会被注解所标识的方法进行处理
    17. * 若浏览器所发送的请求的请求路径和@RequestMapping注解value属性匹配,但是请求方式不匹配
    18. * 此时页面报错:405 - Request method 'xxx' not supported
    19. * 在@RequestMapping的基础上,结合请求方式的一些派生注解:
    20. * @GetMapping,@PostMapping,@DeleteMapping,@PutMapping
    21. * 4、@RequestMapping注解的params属性
    22. * 作用:通过请求的请求参数匹配请求,即浏览器发送的请求的请求参数必须满足params属性的设置
    23. * params可以使用四种表达式:
    24. * "param":表示当前所匹配请求的请求参数中必须携带param参数
    25. * "!param":表示当前所匹配请求的请求参数中一定不能携带param参数
    26. * "param=value":表示当前所匹配请求的请求参数中必须携带param参数且值必须为value
    27. * "param!=value":表示当前所匹配请求的请求参数中可以不携带param,若携带值一定不能是value
    28. * 若浏览器所发送的请求的请求路径和@RequestMapping注解value属性匹配,但是请求参数不匹配
    29. * 此时页面报错:400 - Parameter conditions "username" not met for actual request parameters:
    30. *
    31. *
    32. *
    33. * */
    34. @Controller
    35. //@RequestMapping("/test") //设置当前映射的初始信息
    36. public class TestRequestMappingController {
    37. //此时控制器方法所匹配的请求的请求路径为/test/hello
    38. @RequestMapping(
    39. value = {"/hello","/abc"},
    40. //method = RequestMethod.GET
    41. // method = RequestMethod.POST
    42. method = {RequestMethod.POST, RequestMethod.GET},
    43. // params = {"username"}
    44. params = {"username","!password"}
    45. )
    46. public String hello() {
    47. return "success";
    48. }
    49. }

    现在我们把它加个&password

     第三种方式

     

    1. package com.atguigu.controller;
    2. import org.springframework.stereotype.Controller;
    3. import org.springframework.web.bind.annotation.RequestMapping;
    4. import org.springframework.web.bind.annotation.RequestMethod;
    5. /*
    6. * 1、@RequestMapping注解标识的位置
    7. * @RequestMapping标识一个类:设置映射请求的请求路径的初始信息
    8. * @RequestMapping标识一个方法:设置映射请求请求路径的具体信息
    9. * 2、@RequestMapping注解value属性
    10. * 作用:通过请求的请求路径匹配请求
    11. * value属性是数组类型,即当前浏览器所发送请求的请求路径匹配value属性中的任何一个值
    12. * 则当前请求就会被注解所标识的方法进行处理
    13. * 3、@RequestMapping注解的method属性
    14. * 作用:通过请求的请求方式匹配请求
    15. * method属性是RequestMethod类型的数组,即当前浏览器所发送请求的请求方式匹配method属性中的任何一中请求方式
    16. * 则当前请求就会被注解所标识的方法进行处理
    17. * 若浏览器所发送的请求的请求路径和@RequestMapping注解value属性匹配,但是请求方式不匹配
    18. * 此时页面报错:405 - Request method 'xxx' not supported
    19. * 在@RequestMapping的基础上,结合请求方式的一些派生注解:
    20. * @GetMapping,@PostMapping,@DeleteMapping,@PutMapping
    21. * 4、@RequestMapping注解的params属性
    22. * 作用:通过请求的请求参数匹配请求,即浏览器发送的请求的请求参数必须满足params属性的设置
    23. * params可以使用四种表达式:
    24. * "param":表示当前所匹配请求的请求参数中必须携带param参数
    25. * "!param":表示当前所匹配请求的请求参数中一定不能携带param参数
    26. * "param=value":表示当前所匹配请求的请求参数中必须携带param参数且值必须为value
    27. * "param!=value":表示当前所匹配请求的请求参数中可以不携带param,若携带值一定不能是value
    28. * 若浏览器所发送的请求的请求路径和@RequestMapping注解value属性匹配,但是请求参数不匹配
    29. * 此时页面报错:400 - Parameter conditions "username" not met for actual request parameters:
    30. *
    31. *
    32. *
    33. * */
    34. @Controller
    35. //@RequestMapping("/test") //设置当前映射的初始信息
    36. public class TestRequestMappingController {
    37. //此时控制器方法所匹配的请求的请求路径为/test/hello
    38. @RequestMapping(
    39. value = {"/hello","/abc"},
    40. //method = RequestMethod.GET
    41. // method = RequestMethod.POST
    42. method = {RequestMethod.POST, RequestMethod.GET},
    43. // params = {"username"}
    44. //params = {"username","!password"}
    45. params = {"username","!password","age=20"}
    46. )
    47. public String hello() {
    48. return "success";
    49. }
    50. }

     

     

    现在我们加上&age=20

    第四种方式

     

    1. package com.atguigu.controller;
    2. import org.springframework.stereotype.Controller;
    3. import org.springframework.web.bind.annotation.RequestMapping;
    4. import org.springframework.web.bind.annotation.RequestMethod;
    5. /*
    6. * 1、@RequestMapping注解标识的位置
    7. * @RequestMapping标识一个类:设置映射请求的请求路径的初始信息
    8. * @RequestMapping标识一个方法:设置映射请求请求路径的具体信息
    9. * 2、@RequestMapping注解value属性
    10. * 作用:通过请求的请求路径匹配请求
    11. * value属性是数组类型,即当前浏览器所发送请求的请求路径匹配value属性中的任何一个值
    12. * 则当前请求就会被注解所标识的方法进行处理
    13. * 3、@RequestMapping注解的method属性
    14. * 作用:通过请求的请求方式匹配请求
    15. * method属性是RequestMethod类型的数组,即当前浏览器所发送请求的请求方式匹配method属性中的任何一中请求方式
    16. * 则当前请求就会被注解所标识的方法进行处理
    17. * 若浏览器所发送的请求的请求路径和@RequestMapping注解value属性匹配,但是请求方式不匹配
    18. * 此时页面报错:405 - Request method 'xxx' not supported
    19. * 在@RequestMapping的基础上,结合请求方式的一些派生注解:
    20. * @GetMapping,@PostMapping,@DeleteMapping,@PutMapping
    21. * 4、@RequestMapping注解的params属性
    22. * 作用:通过请求的请求参数匹配请求,即浏览器发送的请求的请求参数必须满足params属性的设置
    23. * params可以使用四种表达式:
    24. * "param":表示当前所匹配请求的请求参数中必须携带param参数
    25. * "!param":表示当前所匹配请求的请求参数中一定不能携带param参数
    26. * "param=value":表示当前所匹配请求的请求参数中必须携带param参数且值必须为value
    27. * "param!=value":表示当前所匹配请求的请求参数中可以不携带param,若携带值一定不能是value
    28. * 若浏览器所发送的请求的请求路径和@RequestMapping注解value属性匹配,但是请求参数不匹配
    29. * 此时页面报错:400 - Parameter conditions "username" not met for actual request parameters:
    30. *
    31. *
    32. *
    33. * */
    34. @Controller
    35. //@RequestMapping("/test") //设置当前映射的初始信息
    36. public class TestRequestMappingController {
    37. //此时控制器方法所匹配的请求的请求路径为/test/hello
    38. @RequestMapping(
    39. value = {"/hello","/abc"},
    40. //method = RequestMethod.GET
    41. // method = RequestMethod.POST
    42. method = {RequestMethod.POST, RequestMethod.GET},
    43. // params = {"username"}
    44. //params = {"username","!password"}
    45. //params = {"username","!password","age=20"}
    46. params = {"username","!password","age=20","gender!=女"}
    47. )
    48. public String hello() {
    49. return "success";
    50. }
    51. }

    现在加上&gender=女

     
     

    总结:

    @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注解的headers属性(了解)

    @RequestMapping 注解的 headers 属性通过请求的请求头信息匹配请求映射
    @RequestMapping 注解的 headers 属性是一个字符串类型的数组,可以通过四种表达式设置请求头信息和请求映射的匹配关系
    "header" :要求请求映射所匹配的请求必须携带 header 请求头信息
    "!header" :要求请求映射所匹配的请求必须不能携带 header 请求头信息
    "header=value" :要求请求映射所匹配的请求必须携带 header 请求头信息且 header=value
    "header!=value" :要求请求映射所匹配的请求必须携带 header 请求头信息且 header!=value
    若当前请求满足 @RequestMapping 注解的 value method 属性,但是不满足 headers 属性,此时页面显示 404 错误,即资源未找到

     

    1. package com.atguigu.controller;
    2. import org.springframework.stereotype.Controller;
    3. import org.springframework.web.bind.annotation.RequestMapping;
    4. import org.springframework.web.bind.annotation.RequestMethod;
    5. /*
    6. * 1、@RequestMapping注解标识的位置
    7. * @RequestMapping标识一个类:设置映射请求的请求路径的初始信息
    8. * @RequestMapping标识一个方法:设置映射请求请求路径的具体信息
    9. * 2、@RequestMapping注解value属性
    10. * 作用:通过请求的请求路径匹配请求
    11. * value属性是数组类型,即当前浏览器所发送请求的请求路径匹配value属性中的任何一个值
    12. * 则当前请求就会被注解所标识的方法进行处理
    13. * 3、@RequestMapping注解的method属性
    14. * 作用:通过请求的请求方式匹配请求
    15. * method属性是RequestMethod类型的数组,即当前浏览器所发送请求的请求方式匹配method属性中的任何一中请求方式
    16. * 则当前请求就会被注解所标识的方法进行处理
    17. * 若浏览器所发送的请求的请求路径和@RequestMapping注解value属性匹配,但是请求方式不匹配
    18. * 此时页面报错:405 - Request method 'xxx' not supported
    19. * 在@RequestMapping的基础上,结合请求方式的一些派生注解:
    20. * @GetMapping,@PostMapping,@DeleteMapping,@PutMapping
    21. * 4、@RequestMapping注解的params属性
    22. * 作用:通过请求的请求参数匹配请求,即浏览器发送的请求的请求参数必须满足params属性的设置
    23. * params可以使用四种表达式:
    24. * "param":表示当前所匹配请求的请求参数中必须携带param参数
    25. * "!param":表示当前所匹配请求的请求参数中一定不能携带param参数
    26. * "param=value":表示当前所匹配请求的请求参数中必须携带param参数且值必须为value
    27. * "param!=value":表示当前所匹配请求的请求参数中可以不携带param,若携带值一定不能是value
    28. * 若浏览器所发送的请求的请求路径和@RequestMapping注解value属性匹配,但是请求参数不匹配
    29. * 此时页面报错:400 - Parameter conditions "username" not met for actual request parameters:
    30. *5、@RequestMapping注解的headers属性
    31. * 作用:通过请求的请求头信息匹配请求,即浏览器发送的请求的请求头信息必须满足headers属性的设置
    32. * 若浏览器所发送的请求的请求路径和@RequestMapping注解value属性匹配,但是请求头信息不匹配
    33. * 此时页面报错:404
    34. *
    35. *
    36. * */
    37. @Controller
    38. //@RequestMapping("/test") //设置当前映射的初始信息
    39. public class TestRequestMappingController {
    40. //此时控制器方法所匹配的请求的请求路径为/test/hello
    41. @RequestMapping(
    42. value = {"/hello","/abc"},
    43. //method = RequestMethod.GET
    44. // method = RequestMethod.POST
    45. method = {RequestMethod.POST, RequestMethod.GET},
    46. // params = {"username"}
    47. //params = {"username","!password"}
    48. //params = {"username","!password","age=20"}
    49. // params = {"username","!password","age=20","gender!=女"}
    50. headers = {"referer"}
    51. )
    52. public String hello() {
    53. return "success";
    54. }
    55. }

    打开F12

     

     

    如果这个时候 我们复制地址值  新建一个标签复制进去后  会报错

    有首页的情况

    总结:

    @RequestMapping注解的headers属性
    * 作用:通过请求的请求头信息匹配请求,即浏览器发送的请求的请求头信息必须满足headers属性的设置
    * 若浏览器所发送的请求的请求路径和@RequestMapping注解value属性匹配,但是请求头信息不匹配
    * 此时页面报错:404

    八   SpringMVC支持ant风格的路径

    ?:表示任意的单个字符
    * :表示任意的 0 个或多个字符
    ** :表示任意层数的任意目录
    注意:在使用 ** 时,只能使用 /**/xxx 的方式

     

    1 .     ?:表示任意的单个字符

    1. package com.atguigu.controller;
    2. import org.springframework.stereotype.Controller;
    3. import org.springframework.web.bind.annotation.RequestMapping;
    4. import org.springframework.web.bind.annotation.RequestMethod;
    5. /*
    6. * 1、@RequestMapping注解标识的位置
    7. * @RequestMapping标识一个类:设置映射请求的请求路径的初始信息
    8. * @RequestMapping标识一个方法:设置映射请求请求路径的具体信息
    9. * 2、@RequestMapping注解value属性
    10. * 作用:通过请求的请求路径匹配请求
    11. * value属性是数组类型,即当前浏览器所发送请求的请求路径匹配value属性中的任何一个值
    12. * 则当前请求就会被注解所标识的方法进行处理
    13. * 3、@RequestMapping注解的method属性
    14. * 作用:通过请求的请求方式匹配请求
    15. * method属性是RequestMethod类型的数组,即当前浏览器所发送请求的请求方式匹配method属性中的任何一中请求方式
    16. * 则当前请求就会被注解所标识的方法进行处理
    17. * 若浏览器所发送的请求的请求路径和@RequestMapping注解value属性匹配,但是请求方式不匹配
    18. * 此时页面报错:405 - Request method 'xxx' not supported
    19. * 在@RequestMapping的基础上,结合请求方式的一些派生注解:
    20. * @GetMapping,@PostMapping,@DeleteMapping,@PutMapping
    21. * 4、@RequestMapping注解的params属性
    22. * 作用:通过请求的请求参数匹配请求,即浏览器发送的请求的请求参数必须满足params属性的设置
    23. * params可以使用四种表达式:
    24. * "param":表示当前所匹配请求的请求参数中必须携带param参数
    25. * "!param":表示当前所匹配请求的请求参数中一定不能携带param参数
    26. * "param=value":表示当前所匹配请求的请求参数中必须携带param参数且值必须为value
    27. * "param!=value":表示当前所匹配请求的请求参数中可以不携带param,若携带值一定不能是value
    28. * 若浏览器所发送的请求的请求路径和@RequestMapping注解value属性匹配,但是请求参数不匹配
    29. * 此时页面报错:400 - Parameter conditions "username" not met for actual request parameters:
    30. *5、@RequestMapping注解的headers属性
    31. * 作用:通过请求的请求头信息匹配请求,即浏览器发送的请求的请求头信息必须满足headers属性的设置
    32. * 若浏览器所发送的请求的请求路径和@RequestMapping注解value属性匹配,但是请求头信息不匹配
    33. * 此时页面报错:404
    34. 6、SpringMVC支持ant风格的路径
    35. * 在@RequestMapping注解的value属性值中设置一些特殊字符
    36. * ?:任意的单个字符(不包括?)
    37. * *:任意个数的任意字符(不包括?和/)
    38. * **:任意层数的任意目录,注意使用方式只能**写在双斜线中,前后不能有任何的其他字符
    39. *
    40. *
    41. * */
    42. @Controller
    43. //@RequestMapping("/test") //设置当前映射的初始信息
    44. public class TestRequestMappingController {
    45. //此时控制器方法所匹配的请求的请求路径为/test/hello
    46. @RequestMapping(
    47. value = {"/hello","/abc"},
    48. //method = RequestMethod.GET
    49. // method = RequestMethod.POST
    50. method = {RequestMethod.POST, RequestMethod.GET},
    51. // params = {"username"}
    52. //params = {"username","!password"}
    53. //params = {"username","!password","age=20"}
    54. // params = {"username","!password","age=20","gender!=女"}
    55. headers = {"referer"}
    56. )
    57. public String hello() {
    58. return "success";
    59. }
    60. @RequestMapping("/a?a/test/ant")
    61. public String testAnt(){
    62. return "success";
    63. }
    64. }

     

    1. html>
    2. <html lang="en" xmlns:th="http://www.thymeleaf.org">
    3. <head>
    4. <meta charset="UTF-8">
    5. <title>首页title>
    6. head>
    7. <body>
    8. <h1>index.htmlh1>
    9. <a th:href="@{/hello}">测试@RequestMapping注解所标识的位置a><br>
    10. <a th:href="@{/abc}">测试@RequestMapping注解的value属性a><br>
    11. <form th:action="@{/hello}" method="post">
    12. <input type="submit" value="测试@RequestMapping注解的method属性">
    13. form>
    14. <a th:href="@{/hello?username=admin}">测试@RequestMapping注解的params属性a><br>
    15. <a th:href="@{/hello(username='admin')}">测试@RequestMapping注解的params属性a><br>
    16. <a th:href="@{/aaa/test/ant}">测试@RequestMapping注解支持ant风格的路径a><br>
    17. body>
    18. html>

     

     但是不能写问号? 或是多个字符 如下所示    则会报错

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

     

    1. package com.atguigu.controller;
    2. import org.springframework.stereotype.Controller;
    3. import org.springframework.web.bind.annotation.RequestMapping;
    4. import org.springframework.web.bind.annotation.RequestMethod;
    5. /*
    6. * 1、@RequestMapping注解标识的位置
    7. * @RequestMapping标识一个类:设置映射请求的请求路径的初始信息
    8. * @RequestMapping标识一个方法:设置映射请求请求路径的具体信息
    9. * 2、@RequestMapping注解value属性
    10. * 作用:通过请求的请求路径匹配请求
    11. * value属性是数组类型,即当前浏览器所发送请求的请求路径匹配value属性中的任何一个值
    12. * 则当前请求就会被注解所标识的方法进行处理
    13. * 3、@RequestMapping注解的method属性
    14. * 作用:通过请求的请求方式匹配请求
    15. * method属性是RequestMethod类型的数组,即当前浏览器所发送请求的请求方式匹配method属性中的任何一中请求方式
    16. * 则当前请求就会被注解所标识的方法进行处理
    17. * 若浏览器所发送的请求的请求路径和@RequestMapping注解value属性匹配,但是请求方式不匹配
    18. * 此时页面报错:405 - Request method 'xxx' not supported
    19. * 在@RequestMapping的基础上,结合请求方式的一些派生注解:
    20. * @GetMapping,@PostMapping,@DeleteMapping,@PutMapping
    21. * 4、@RequestMapping注解的params属性
    22. * 作用:通过请求的请求参数匹配请求,即浏览器发送的请求的请求参数必须满足params属性的设置
    23. * params可以使用四种表达式:
    24. * "param":表示当前所匹配请求的请求参数中必须携带param参数
    25. * "!param":表示当前所匹配请求的请求参数中一定不能携带param参数
    26. * "param=value":表示当前所匹配请求的请求参数中必须携带param参数且值必须为value
    27. * "param!=value":表示当前所匹配请求的请求参数中可以不携带param,若携带值一定不能是value
    28. * 若浏览器所发送的请求的请求路径和@RequestMapping注解value属性匹配,但是请求参数不匹配
    29. * 此时页面报错:400 - Parameter conditions "username" not met for actual request parameters:
    30. *5、@RequestMapping注解的headers属性
    31. * 作用:通过请求的请求头信息匹配请求,即浏览器发送的请求的请求头信息必须满足headers属性的设置
    32. * 若浏览器所发送的请求的请求路径和@RequestMapping注解value属性匹配,但是请求头信息不匹配
    33. * 此时页面报错:404
    34. 6、SpringMVC支持ant风格的路径
    35. * 在@RequestMapping注解的value属性值中设置一些特殊字符
    36. * ?:任意的单个字符(不包括?)
    37. * *:任意个数的任意字符(不包括?和/)
    38. * **:任意层数的任意目录,注意使用方式只能**写在双斜线中,前后不能有任何的其他字符
    39. *
    40. *
    41. * */
    42. @Controller
    43. //@RequestMapping("/test") //设置当前映射的初始信息
    44. public class TestRequestMappingController {
    45. //此时控制器方法所匹配的请求的请求路径为/test/hello
    46. @RequestMapping(
    47. value = {"/hello","/abc"},
    48. //method = RequestMethod.GET
    49. // method = RequestMethod.POST
    50. method = {RequestMethod.POST, RequestMethod.GET},
    51. // params = {"username"}
    52. //params = {"username","!password"}
    53. //params = {"username","!password","age=20"}
    54. // params = {"username","!password","age=20","gender!=女"}
    55. headers = {"referer"}
    56. )
    57. public String hello() {
    58. return "success";
    59. }
    60. @RequestMapping("/a*a/test/ant")
    61. public String testAnt(){
    62. return "success";
    63. }
    64. }

     

     

     

    但是不能写?和/

    3 .      **:表示任意层数的任意目录

     

    1. package com.atguigu.controller;
    2. import org.springframework.stereotype.Controller;
    3. import org.springframework.web.bind.annotation.RequestMapping;
    4. import org.springframework.web.bind.annotation.RequestMethod;
    5. /*
    6. * 1、@RequestMapping注解标识的位置
    7. * @RequestMapping标识一个类:设置映射请求的请求路径的初始信息
    8. * @RequestMapping标识一个方法:设置映射请求请求路径的具体信息
    9. * 2、@RequestMapping注解value属性
    10. * 作用:通过请求的请求路径匹配请求
    11. * value属性是数组类型,即当前浏览器所发送请求的请求路径匹配value属性中的任何一个值
    12. * 则当前请求就会被注解所标识的方法进行处理
    13. * 3、@RequestMapping注解的method属性
    14. * 作用:通过请求的请求方式匹配请求
    15. * method属性是RequestMethod类型的数组,即当前浏览器所发送请求的请求方式匹配method属性中的任何一中请求方式
    16. * 则当前请求就会被注解所标识的方法进行处理
    17. * 若浏览器所发送的请求的请求路径和@RequestMapping注解value属性匹配,但是请求方式不匹配
    18. * 此时页面报错:405 - Request method 'xxx' not supported
    19. * 在@RequestMapping的基础上,结合请求方式的一些派生注解:
    20. * @GetMapping,@PostMapping,@DeleteMapping,@PutMapping
    21. * 4、@RequestMapping注解的params属性
    22. * 作用:通过请求的请求参数匹配请求,即浏览器发送的请求的请求参数必须满足params属性的设置
    23. * params可以使用四种表达式:
    24. * "param":表示当前所匹配请求的请求参数中必须携带param参数
    25. * "!param":表示当前所匹配请求的请求参数中一定不能携带param参数
    26. * "param=value":表示当前所匹配请求的请求参数中必须携带param参数且值必须为value
    27. * "param!=value":表示当前所匹配请求的请求参数中可以不携带param,若携带值一定不能是value
    28. * 若浏览器所发送的请求的请求路径和@RequestMapping注解value属性匹配,但是请求参数不匹配
    29. * 此时页面报错:400 - Parameter conditions "username" not met for actual request parameters:
    30. *5、@RequestMapping注解的headers属性
    31. * 作用:通过请求的请求头信息匹配请求,即浏览器发送的请求的请求头信息必须满足headers属性的设置
    32. * 若浏览器所发送的请求的请求路径和@RequestMapping注解value属性匹配,但是请求头信息不匹配
    33. * 此时页面报错:404
    34. 6、SpringMVC支持ant风格的路径
    35. * 在@RequestMapping注解的value属性值中设置一些特殊字符
    36. * ?:任意的单个字符(不包括?)
    37. * *:任意个数的任意字符(不包括?和/)
    38. * **:任意层数的任意目录,注意使用方式只能**写在双斜线中,前后不能有任何的其他字符
    39. *
    40. *
    41. * */
    42. @Controller
    43. //@RequestMapping("/test") //设置当前映射的初始信息
    44. public class TestRequestMappingController {
    45. //此时控制器方法所匹配的请求的请求路径为/test/hello
    46. @RequestMapping(
    47. value = {"/hello","/abc"},
    48. //method = RequestMethod.GET
    49. // method = RequestMethod.POST
    50. method = {RequestMethod.POST, RequestMethod.GET},
    51. // params = {"username"}
    52. //params = {"username","!password"}
    53. //params = {"username","!password","age=20"}
    54. // params = {"username","!password","age=20","gender!=女"}
    55. headers = {"referer"}
    56. )
    57. public String hello() {
    58. return "success";
    59. }
    60. /* @RequestMapping("/a?a/test/ant")
    61. public String testAnt(){
    62. return "success";
    63. }*/
    64. /* @RequestMapping("/a*a/test/ant")
    65. public String testAnt(){
    66. return "success";
    67. }*/
    68. @RequestMapping("/**/test/ant")
    69. public String testAnt(){
    70. return "success";
    71. }
    72. }

     

    总结:

    SpringMVC支持ant风格的路径
    * 在@RequestMapping注解的value属性值中设置一些特殊字符
    * ?:任意的单个字符(不包括?)
    * *:任意个数的任意字符(不包括?和/)
    * **:任意层数的任意目录,注意使用方式只能**写在双斜线中,前后不能有任何的其他字符

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

    原始方式: /deleteUser?id=1
    rest 方式: /user/delete/1
    SpringMVC 路径中的占位符常用于 RESTful 风格中,当请求路径中将某些数据通过路径的方式传输到服务器中,就可以在相应的 @RequestMapping 注解的 value 属性中通过占位符 {xxx} 表示传输的数据,在 通过 @PathVariable 注解,将占位符所表示的数据赋值给控制器方法的形参

     

    1. package com.atguigu.controller;
    2. import org.springframework.stereotype.Controller;
    3. import org.springframework.web.bind.annotation.PathVariable;
    4. import org.springframework.web.bind.annotation.RequestMapping;
    5. import org.springframework.web.bind.annotation.RequestMethod;
    6. /*
    7. * 1、@RequestMapping注解标识的位置
    8. * @RequestMapping标识一个类:设置映射请求的请求路径的初始信息
    9. * @RequestMapping标识一个方法:设置映射请求请求路径的具体信息
    10. * 2、@RequestMapping注解value属性
    11. * 作用:通过请求的请求路径匹配请求
    12. * value属性是数组类型,即当前浏览器所发送请求的请求路径匹配value属性中的任何一个值
    13. * 则当前请求就会被注解所标识的方法进行处理
    14. * 3、@RequestMapping注解的method属性
    15. * 作用:通过请求的请求方式匹配请求
    16. * method属性是RequestMethod类型的数组,即当前浏览器所发送请求的请求方式匹配method属性中的任何一中请求方式
    17. * 则当前请求就会被注解所标识的方法进行处理
    18. * 若浏览器所发送的请求的请求路径和@RequestMapping注解value属性匹配,但是请求方式不匹配
    19. * 此时页面报错:405 - Request method 'xxx' not supported
    20. * 在@RequestMapping的基础上,结合请求方式的一些派生注解:
    21. * @GetMapping,@PostMapping,@DeleteMapping,@PutMapping
    22. * 4、@RequestMapping注解的params属性
    23. * 作用:通过请求的请求参数匹配请求,即浏览器发送的请求的请求参数必须满足params属性的设置
    24. * params可以使用四种表达式:
    25. * "param":表示当前所匹配请求的请求参数中必须携带param参数
    26. * "!param":表示当前所匹配请求的请求参数中一定不能携带param参数
    27. * "param=value":表示当前所匹配请求的请求参数中必须携带param参数且值必须为value
    28. * "param!=value":表示当前所匹配请求的请求参数中可以不携带param,若携带值一定不能是value
    29. * 若浏览器所发送的请求的请求路径和@RequestMapping注解value属性匹配,但是请求参数不匹配
    30. * 此时页面报错:400 - Parameter conditions "username" not met for actual request parameters:
    31. *5、@RequestMapping注解的headers属性
    32. * 作用:通过请求的请求头信息匹配请求,即浏览器发送的请求的请求头信息必须满足headers属性的设置
    33. * 若浏览器所发送的请求的请求路径和@RequestMapping注解value属性匹配,但是请求头信息不匹配
    34. * 此时页面报错:404
    35. 6、SpringMVC支持ant风格的路径
    36. * 在@RequestMapping注解的value属性值中设置一些特殊字符
    37. * ?:任意的单个字符(不包括?)
    38. * *:任意个数的任意字符(不包括?和/)
    39. * **:任意层数的任意目录,注意使用方式只能**写在双斜线中,前后不能有任何的其他字符
    40. *7、@RequestMapping注解使用路径中的占位符
    41. * 传统:/deleteUser?id=1
    42. * rest:/user/delete/1
    43. * 需要在@RequestMapping注解的value属性中所设置的路径中,使用{xxx}的方式表示路径中的数据
    44. * 在通过@PathVariable注解,将占位符所标识的值和控制器方法的形参进行绑定
    45. * */
    46. @Controller
    47. //@RequestMapping("/test") //设置当前映射的初始信息
    48. public class TestRequestMappingController {
    49. //此时控制器方法所匹配的请求的请求路径为/test/hello
    50. @RequestMapping(
    51. value = {"/hello","/abc"},
    52. //method = RequestMethod.GET
    53. // method = RequestMethod.POST
    54. method = {RequestMethod.POST, RequestMethod.GET},
    55. // params = {"username"}
    56. //params = {"username","!password"}
    57. //params = {"username","!password","age=20"}
    58. // params = {"username","!password","age=20","gender!=女"}
    59. headers = {"referer"}
    60. )
    61. public String hello() {
    62. return "success";
    63. }
    64. /* @RequestMapping("/a?a/test/ant")
    65. public String testAnt(){
    66. return "success";
    67. }*/
    68. /* @RequestMapping("/a*a/test/ant")
    69. public String testAnt(){
    70. return "success";
    71. }*/
    72. @RequestMapping("/**/test/ant")
    73. public String testAnt(){
    74. return "success";
    75. }
    76. /* @RequestMapping("/test/rest/{username}/{id}")
    77. public String testRest(@PathVariable("id") Integer id, @PathVariable("username") String username){
    78. System.out.println("id:"+id+",username:"+username);
    79. return "success";
    80. }*/
    81. @RequestMapping("/test/rest/{id}")
    82. public String testRest(@PathVariable("id") Integer id){
    83. System.out.println("id:"+id);
    84. return "success";
    85. }
    86. }

     

    1. html>
    2. <html lang="en" xmlns:th="http://www.thymeleaf.org">
    3. <head>
    4. <meta charset="UTF-8">
    5. <title>首页title>
    6. head>
    7. <body>
    8. <h1>index.htmlh1>
    9. <a th:href="@{/hello}">测试@RequestMapping注解所标识的位置a><br>
    10. <a th:href="@{/abc}">测试@RequestMapping注解的value属性a><br>
    11. <form th:action="@{/hello}" method="post">
    12. <input type="submit" value="测试@RequestMapping注解的method属性">
    13. form>
    14. <a th:href="@{/hello?username=admin}">测试@RequestMapping注解的params属性a><br>
    15. <a th:href="@{/hello(username='admin')}">测试@RequestMapping注解的params属性a><br>
    16. <a th:href="@{/aaa/test/ant}">测试@RequestMapping注解支持ant风格的路径a><br>
    17. <a th:href="@{/test/rest/1}">测试@RequestMapping注解的value属性中的占位符a><br>
    18. body>
    19. html>

     

    现在我们加上username

    1. package com.atguigu.controller;
    2. import org.springframework.stereotype.Controller;
    3. import org.springframework.web.bind.annotation.PathVariable;
    4. import org.springframework.web.bind.annotation.RequestMapping;
    5. import org.springframework.web.bind.annotation.RequestMethod;
    6. /*
    7. * 1、@RequestMapping注解标识的位置
    8. * @RequestMapping标识一个类:设置映射请求的请求路径的初始信息
    9. * @RequestMapping标识一个方法:设置映射请求请求路径的具体信息
    10. * 2、@RequestMapping注解value属性
    11. * 作用:通过请求的请求路径匹配请求
    12. * value属性是数组类型,即当前浏览器所发送请求的请求路径匹配value属性中的任何一个值
    13. * 则当前请求就会被注解所标识的方法进行处理
    14. * 3、@RequestMapping注解的method属性
    15. * 作用:通过请求的请求方式匹配请求
    16. * method属性是RequestMethod类型的数组,即当前浏览器所发送请求的请求方式匹配method属性中的任何一中请求方式
    17. * 则当前请求就会被注解所标识的方法进行处理
    18. * 若浏览器所发送的请求的请求路径和@RequestMapping注解value属性匹配,但是请求方式不匹配
    19. * 此时页面报错:405 - Request method 'xxx' not supported
    20. * 在@RequestMapping的基础上,结合请求方式的一些派生注解:
    21. * @GetMapping,@PostMapping,@DeleteMapping,@PutMapping
    22. * 4、@RequestMapping注解的params属性
    23. * 作用:通过请求的请求参数匹配请求,即浏览器发送的请求的请求参数必须满足params属性的设置
    24. * params可以使用四种表达式:
    25. * "param":表示当前所匹配请求的请求参数中必须携带param参数
    26. * "!param":表示当前所匹配请求的请求参数中一定不能携带param参数
    27. * "param=value":表示当前所匹配请求的请求参数中必须携带param参数且值必须为value
    28. * "param!=value":表示当前所匹配请求的请求参数中可以不携带param,若携带值一定不能是value
    29. * 若浏览器所发送的请求的请求路径和@RequestMapping注解value属性匹配,但是请求参数不匹配
    30. * 此时页面报错:400 - Parameter conditions "username" not met for actual request parameters:
    31. *5、@RequestMapping注解的headers属性
    32. * 作用:通过请求的请求头信息匹配请求,即浏览器发送的请求的请求头信息必须满足headers属性的设置
    33. * 若浏览器所发送的请求的请求路径和@RequestMapping注解value属性匹配,但是请求头信息不匹配
    34. * 此时页面报错:404
    35. 6、SpringMVC支持ant风格的路径
    36. * 在@RequestMapping注解的value属性值中设置一些特殊字符
    37. * ?:任意的单个字符(不包括?)
    38. * *:任意个数的任意字符(不包括?和/)
    39. * **:任意层数的任意目录,注意使用方式只能**写在双斜线中,前后不能有任何的其他字符
    40. *7、@RequestMapping注解使用路径中的占位符
    41. * 传统:/deleteUser?id=1
    42. * rest:/user/delete/1
    43. * 需要在@RequestMapping注解的value属性中所设置的路径中,使用{xxx}的方式表示路径中的数据
    44. * 在通过@PathVariable注解,将占位符所标识的值和控制器方法的形参进行绑定
    45. * */
    46. @Controller
    47. //@RequestMapping("/test") //设置当前映射的初始信息
    48. public class TestRequestMappingController {
    49. //此时控制器方法所匹配的请求的请求路径为/test/hello
    50. @RequestMapping(
    51. value = {"/hello","/abc"},
    52. //method = RequestMethod.GET
    53. // method = RequestMethod.POST
    54. method = {RequestMethod.POST, RequestMethod.GET},
    55. // params = {"username"}
    56. //params = {"username","!password"}
    57. //params = {"username","!password","age=20"}
    58. // params = {"username","!password","age=20","gender!=女"}
    59. headers = {"referer"}
    60. )
    61. public String hello() {
    62. return "success";
    63. }
    64. /* @RequestMapping("/a?a/test/ant")
    65. public String testAnt(){
    66. return "success";
    67. }*/
    68. /* @RequestMapping("/a*a/test/ant")
    69. public String testAnt(){
    70. return "success";
    71. }*/
    72. @RequestMapping("/**/test/ant")
    73. public String testAnt(){
    74. return "success";
    75. }
    76. /* @RequestMapping("/test/rest/{id}")
    77. public String testRest(@PathVariable("id") Integer id){
    78. System.out.println("id:"+id);
    79. return "success";
    80. }*/
    81. @RequestMapping("/test/rest/{username}/{id}")
    82. public String testRest(@PathVariable("id") Integer id, @PathVariable("username") String username){
    83. System.out.println("id:"+id+",username:"+username);
    84. return "success";
    85. }
    86. }
     
       

    1. html>
    2. <html lang="en" xmlns:th="http://www.thymeleaf.org">
    3. <head>
    4. <meta charset="UTF-8">
    5. <title>首页title>
    6. head>
    7. <body>
    8. <h1>index.htmlh1>
    9. <a th:href="@{/hello}">测试@RequestMapping注解所标识的位置a><br>
    10. <a th:href="@{/abc}">测试@RequestMapping注解的value属性a><br>
    11. <form th:action="@{/hello}" method="post">
    12. <input type="submit" value="测试@RequestMapping注解的method属性">
    13. form>
    14. <a th:href="@{/hello?username=admin}">测试@RequestMapping注解的params属性a><br>
    15. <a th:href="@{/hello(username='admin')}">测试@RequestMapping注解的params属性a><br>
    16. <a th:href="@{/aaa/test/ant}">测试@RequestMapping注解支持ant风格的路径a><br>
    17. <a th:href="@{/test/rest/admin/1}">测试@RequestMapping注解的value属性中的占位符a><br>
    18. body>
    19. html>

    总结:

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

     全部总结:

    /*
     * 1、@RequestMapping注解标识的位置
     * @RequestMapping标识一个类:设置映射请求的请求路径的初始信息
     * @RequestMapping标识一个方法:设置映射请求请求路径的具体信息
     * 2、@RequestMapping注解value属性
     * 作用:通过请求的请求路径匹配请求
     * value属性是数组类型,即当前浏览器所发送请求的请求路径匹配value属性中的任何一个值
     * 则当前请求就会被注解所标识的方法进行处理
     * 3、@RequestMapping注解的method属性
     * 作用:通过请求的请求方式匹配请求
     * method属性是RequestMethod类型的数组,即当前浏览器所发送请求的请求方式匹配method属性中的任何一中请求方式
     * 则当前请求就会被注解所标识的方法进行处理
     * 若浏览器所发送的请求的请求路径和@RequestMapping注解value属性匹配,但是请求方式不匹配
     * 此时页面报错:405 - Request method 'xxx' not supported
     * 在@RequestMapping的基础上,结合请求方式的一些派生注解:
     * @GetMapping,@PostMapping,@DeleteMapping,@PutMapping
     * 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:
     *5、@RequestMapping注解的headers属性
     * 作用:通过请求的请求头信息匹配请求,即浏览器发送的请求的请求头信息必须满足headers属性的设置
     * 若浏览器所发送的请求的请求路径和@RequestMapping注解value属性匹配,但是请求头信息不匹配
     * 此时页面报错:404
     6、SpringMVC支持ant风格的路径
     * 在@RequestMapping注解的value属性值中设置一些特殊字符
     * ?:任意的单个字符(不包括?)
     * *:任意个数的任意字符(不包括?和/)
     * **:任意层数的任意目录,注意使用方式只能**写在双斜线中,前后不能有任何的其他字符
     *7、@RequestMapping注解使用路径中的占位符
     * 传统:/deleteUser?id=1
     * rest:/user/delete/1
     * 需要在@RequestMapping注解的value属性中所设置的路径中,使用{xxx}的方式表示路径中的数据
     * 在通过@PathVariable注解,将占位符所标识的值和控制器方法的形参进行绑定
    * */

     

     

     

     
     
     
     
     
     
     
     

     
     
     
     

  • 相关阅读:
    pandas常用操作
    【支付漏洞】
    flutter开发的一个小小小问题,内网依赖下不来
    计算机的磁盘与中断介绍
    分布式消息队列RocketMQ概念详解
    configure: error: cannot compute suffix of object files: cannot compile
    ts1.基础类型
    LInux系统编程(3)
    ping多个IP的工具
    LLM大模型:在RAG系统中应用知识图谱
  • 原文地址:https://blog.csdn.net/m0_59281987/article/details/127826334