SpringMVC-@RequestMapping注解
环境
IDE:idea 2020.3
构建工具:maven-3.8.1
服务器:tomcat9
Spring版本:5.3.1
<dependencies>
<!-- SpringMVC -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.1</version>
</dependency>
<!-- 日志 -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</dependency>
<!-- ServletAPI -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<!-- Spring5和Thymeleaf整合包 -->
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring5</artifactId>
<version>3.0.12.RELEASE</version>
</dependency>
</dependencies>
<!-- web.xml -->
<servlet>
<servlet-name>springMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<!--
作为框架的核心组件,在启动过程中有大量的初始化操作要做
而这些操作放在第一次请求时才执行会严重影响访问速度
因此需要通过此标签将启动控制DispatcherServlet的初始化时间提前到服务器启动时
-->
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springMVC.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
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">
<!-- springMVC.xml -->
<!-- 自动扫描包 -->
<context:component-scan base-package="com.fly.controller"/>
<!-- 配置Thymeleaf视图解析器 -->
<bean id="viewResolver" class="org.thymeleaf.spring5.view.ThymeleafViewResolver">
<property name="order" value="1"/>
<property name="characterEncoding" value="UTF-8"/>
<property name="templateEngine">
<bean class="org.thymeleaf.spring5.SpringTemplateEngine">
<property name="templateResolver">
<bean class="org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver">
<!-- 视图前缀 -->
<property name="prefix" value="/WEB-INF/templates/"/>
<!-- 视图后缀 -->
<property name="suffix" value=".html"/>
<property name="templateMode" value="HTML5"/>
<property name="characterEncoding" value="UTF-8" />
</bean>
</property>
</bean>
</property>
</bean>
</beans>
@RequestMapping注解的功能
@RequestMapping注解将请求映射到控制器方法,SpringMVC 接收到指定的请求,就会来找到在映射关系中对应的控制器方法来处理这个请求。
注解的位置
在类上面使用@RequestMapping注解
设置映射请求的请求路径的初始信息
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>首页</title>
</head>
<body>
<a th:href="@{/hello/show}">测试@RequestMapping标识一个类</a>
</body>
</html>
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>hello</title>
</head>
<body>
<h2>hello</h2>
</body>
</html>
package com.fly.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* @author 26414
*/
@Controller
public class TestController {
@RequestMapping("/")
public String index() {
return "index";
}
}
package com.fly.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* @author 26414
*/
@Controller
@RequestMapping("/hello")
public class HelloController {
@RequestMapping("/show")
public String show() {
return "hello";
}
}
方法上使用@RequestMapping注解
设置映射请求请求路径的具体信息
在controller比较多时,可以用@RequestMapping标识一个类,解决映射路径重复的问题。
value属性
value概括
1)SpringMVC通过@RequestMapping注解的value属性的值与请求地址进行匹配
2)value属性是一个String类型的数组,可以设置多个值,匹配多个请求地址所对应的请求
3)@RequestMapping注解的value属性必须设置
ant风格的路径
1)?:表示除?等特殊字符之外任意的单个字符
2):表示任意的0个或多个字符
3)**:表示任意的一层或多层目录
4)在使用*时,只能使用/**/xxx的方式
路径中的占位符
通过@PathVariable注解,将占位符所表示的数据赋值给控制器方法的形参
/**
*HelloController
*/
@RequestMapping("/rest/{id}")
public String testRest(@PathVariable String id) {
System.out.println("id = " + id);
return "hello";
}
method属性
1)@RequestMapping注解的method属性通过请求的请求方式(get或post)匹配请求映射
2)@RequestMapping注解的method属性是一个RequestMethod(枚举)类型的数组,表示该请求映射能够匹配多种请求方式的请求
3)若当前请求的请求地址满足请求映射的value属性,但是请求方式不满足method属性,则浏览器报错405:Request method 'POST' not supported
4)对于处理指定请求方式的控制器方法,SpringMVC中提供了@RequestMapping的派生注解
5)如果没有赋值,不以请求方法匹配请求映射
@GetMapping("/testMethod")
public String testMethod() {
return "hello";
}
<!--index.html-->
<form th:action="@{/testMethod}" method="post">
<input type="submit">
</form>
params属性
headers属性