ToB企服应用市场:ToB评测及商务社交产业平台

标题: 3、SpringMVC之RequestMapping注解 [打印本页]

作者: 天津储鑫盛钢材现货供应商    时间: 2023-10-6 19:53
标题: 3、SpringMVC之RequestMapping注解
3.1、环境搭建

创建名为spring_mvc_demo的新module,过程参考2.1节
3.1.1、创建SpringMVC的配置文件

  1. <?xml version="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.    
  7.     <context:component-scan base-package="org.rain.controller"></context:component-scan>
  8.    
  9.     <bean id="viewResolver" >
  10.         <property name="order" value="1"/>
  11.         <property name="characterEncoding" value="UTF-8"/>
  12.         <property name="templateEngine">
  13.             <bean >
  14.                 <property name="templateResolver">
  15.                     <bean
  16.                             >
  17.                         
  18.                         <property name="prefix" value="/WEB-INF/templates/"/>
  19.                         
  20.                         <property name="suffix" value=".html"/>
  21.                         <property name="templateMode" value="HTML5"/>
  22.                         <property name="characterEncoding" value="UTF-8" />
  23.                     </bean>
  24.                 </property>
  25.             </bean>
  26.         </property>
  27.     </bean>
  28. </beans>
复制代码
3.1.2、配置web.xml

  1.    
  2.     <servlet>
  3.         <servlet-name>SpringMVC</servlet-name>
  4.         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  5.         
  6.         <init-param>
  7.             <param-name>contextConfigLocation</param-name>
  8.             <param-value>classpath:springmvc.xml</param-value>
  9.         </init-param>
  10.         
  11.         <load-on-startup>1</load-on-startup>
  12.     </servlet>
  13.     <servlet-mapping>
  14.         <servlet-name>SpringMVC</servlet-name>
  15.         <url-pattern>/</url-pattern>
  16.     </servlet-mapping>
复制代码
3.1.3、创建请求控制器

  1. package org.rain.controller;
  2. import org.springframework.stereotype.Controller;
  3. /**
  4. * @author liaojy
  5. * @date 2023/9/21 - 8:47
  6. */
  7. @Controller
  8. public class TestRequestMappingController {
  9. }
复制代码
  1. package org.rain.controller;
  2. import org.springframework.stereotype.Controller;
  3. import org.springframework.web.bind.annotation.RequestMapping;
  4. /**
  5. * @author liaojy
  6. * @date 2023/9/21 - 8:49
  7. */
  8. @Controller
  9. public class PortalController {
  10.     @RequestMapping("/")
  11.     public String portal(){
  12.         return "index";
  13.     }
  14. }
复制代码
3.1.4、创建静态资源目录及页面

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <title>首页</title>
  6. </head>
  7. <body>
  8. <h1>index.html</h1>
  9. </body>
  10. </html>
复制代码
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <title>成功</title>
  6. </head>
  7. <body>
  8. <h1>success.html</h1>
  9. </body>
  10. </html>
复制代码
3.1.5、配置tomcat



细节请参考2.6节
3.2、注解的功能

3.3、注解的位置

3.3.1、源码定义


从源码可知,@RequestMapping注解既可以标识在类上,也可以标识在方法上
3.3.2、控制器示例

  1. @Controller
  2. @RequestMapping("/test")
  3. public class TestRequestMappingController {
  4.     // 此时控制器方法所匹配的请求的请求路径为:/test/hello
  5.     @RequestMapping("/hello")
  6.     public String hello(){
  7.         return "success";
  8.     }
  9. }
复制代码
3.3.3、请求示例


注意html要引入thymeleaf的约束:xmlns:th="http://www.thymeleaf.org"
  1. <a th:target="_blank" href="https://www.cnblogs.com/@{/hello}">测试/hello请求</a>
  2. <a th:target="_blank" href="https://www.cnblogs.com/@{/test/hello}">测试/test/hello请求</a>
复制代码
3.3.4、测试效果





3.3.5、双重位置的作用

可以根据业务需要划分模块,在请求路径中就可以体现出请求的是哪个模块的资源
3.4、注解的value属性

@RequestMapping注解的value属性必须设置,其作用是根据请求路径来匹配请求
3.4.1、源码定义


value属性的别名是path,所以用path属性代替也可以;
value属性是字符串数组类型,所以可以设置多个值;
3.4.2、匹配多个请求的控制器示例


普通的servlet,也可以在web.xml的标签中,设置多个子标签,从而实现同样的效果
  1.     @RequestMapping({"/hello","/hi"})
复制代码
  1. <a th:target="_blank" href="https://www.cnblogs.com/@{/test/hi}">测试/test/hi请求</a>
复制代码
3.4.3、测试效果





3.5、注解的method属性

3.5.1、源码定义


method属性是RequestMethod数组类型,所以可以设置多个值;

RequestMethod是枚举类型,有固定的可选值
3.5.2、控制器示例

  1.     @RequestMapping(value = {"/hello","/hi"},method = {RequestMethod.GET,RequestMethod.POST})
复制代码
3.5.3、请求示例

  1. <a th:target="_blank" href="https://www.cnblogs.com/@{/test/hello}">测试/test/hello请求</a>
  2. <form th:action="@{/test/hello}" method="post">
  3.     <input type="submit" value="测试@RequestMapping注解的method属性的post请求">
  4. </form>
复制代码
3.5.4、测试效果



++++++++++++++++++++++++++分割线++++++++++++++++++++++++++


3.5.5、结合请求方式的派生注解

对于处理指定请求方式的控制器方法,SpringMVC中提供了@RequestMapping的派生注解

  1.     // 只处理post请求方式的请求
  2.     @PostMapping("/hello")
复制代码


++++++++++++++++++++++++++分割线++++++++++++++++++++++++++


3.6、注解的params属性(了解)

3.6.1、源码定义


params属性是字符串数组类型,所以可以设置多个值;
注意:请求必须满足params属性所有值的要求。
3.6.2、params属性的四种表达式

3.6.3、控制器示例

  1.     @RequestMapping(
  2.             value = {"/hello","/hi"},
  3.             method = {RequestMethod.GET,RequestMethod.POST},
  4.             params = {"username","!password","age=18","gender!=女"}
  5.             )
复制代码
3.6.4、请求示例

  1. <a th:target="_blank" href="https://www.cnblogs.com/@{/test/hello?username=admin&age=18}">测试@RequestMapping注解的params属性(传统?传参)</a>
  2. <br><br>
  3. <a th:target="_blank" href="https://www.cnblogs.com/@{/test/hello(username='admin',age=18)}">测试@RequestMapping注解的params属性(thymeleaf语法传参)</a>
复制代码
3.6.5、测试效果



++++++++++++++++++++++++++分割线++++++++++++++++++++++++++


++++++++++++++++++++++++++分割线++++++++++++++++++++++++++


3.7、注解的headers属性(了解)

3.7.1、源码定义


headers属性是字符串数组类型,所以可以设置多个值;
注意:请求必须满足headers属性所有值的要求。
3.7.2、headers属性的四种表达式

3.7.3、控制器示例


注意:请求头信息的键不区分大小写,但其值区分大小写
  1.     @RequestMapping(
  2.             value = {"/hello","/hi"},
  3.             method = {RequestMethod.GET,RequestMethod.POST},
  4.             params = {"username","!password","age=18","gender!=女"},
  5.             headers = {"referer"}
  6.             )
复制代码
3.7.4、请求示例



Referer请求头表示请求的来源,本例的请求来源为http://localhost:8080/spring_mvc_demo/

如果是通过浏览器地址栏直接访问,是没有Referer(请求来源)的
3.8、ant风格的路径

3.8.1、通配符

3.8.2、?  通配符示例

  1.     @RequestMapping("/a?a")
  2.     public String testAnt(){
  3.         return "success";
  4.     }
复制代码



3.8.3、*  通配符示例

  1.     @RequestMapping("/a*a")
  2.     public String testAnt(){
  3.         return "success";
  4.     }
复制代码



3.8.4、** 通配符示例

  1.     @RequestMapping("/**/aa")
  2.     public String testAnt(){
  3.         return "success";
  4.     }
复制代码


3.9、路径中的占位符(重点)

3.9.1、控制器示例


对于占位符的数据类型,@PathVariable注解会自动转换赋值给控制器方法的形参
  1.     @RequestMapping("/rest/{restfulId}/{restfulUsername}")
  2.     public String testRestful(@PathVariable("restfulId") Integer id,@PathVariable("restfulUsername") String username){
  3.         System.out.println("id:"+id);
  4.         System.out.println("username:"+username);
  5.         return "success";
  6.     }
复制代码
3.9.2、请求示例

  1. <a th:target="_blank" href="https://www.cnblogs.com/@{/test/rest/12345/zhangsan}">测试@RequestMapping注解的value属性中的占位符</a>
复制代码
3.9.3、测试效果





免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!




欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/) Powered by Discuz! X3.4