目录
4、@RequestMapping 注解的 value 属性
5、@RequestMapping 注解的 method 属性
6、@RequestMapping注解的params属性(了解)
7、@RequestMapping注解的headers属性(了解)
0、准备工作:创建新模块、配置并测试
创建新模块,目录如下
TestController 类的内容:
@Controller
public class TestController {
@RequestMapping("/")
public String index(){
return "index";
}
}
SpringMVC.xml 的内容:
<?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">
<!-- 扫描组件 -->
<context:component-scan base-package="com.zyj.mvc.controller"></context:component-scan>
<!-- 配置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>
web.xml 的内容:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<!-- 注册前端控制器DispatcherServlet -->
<servlet>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<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>DispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
index.html 的内容:
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>首页</h1>
</body>
</html>
1、@RequestMapping 注解的功能
从注解名称上我们可以看到,@RequestMapping注解的作用就是将请求和处理请求的控制器方法关联起来,建立映射关系。
SpringMVC 接收到指定的请求,就会找到在映射关系中对应的控制器方法来处理这个请求。
2、控制器中有多个方法对应同一个请求的情况 :报错
例如以下两个类 TestController 和 RequestMappingController 中都有方法对应请求 /
@Controller
public class TestController {
@RequestMapping("/")
public String index(){
return "index";
}
}
@Controller
public class RequestMappingController {
@RequestMapping("/")
public String index(){
return "target";
}
}
运行结果报错如下
3、@RequestMapping 注解不同位置的作用
通过查看 @RequestMapping 注解的源码,可以在类的注解 @Target 发现 @RequestMapping 注解可以表示类和方法
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Mapping
public @interface RequestMapping
@RequestMapping标识一个类:设置映射请求的请求路径的初始信息
@RequestMapping标识一个方法:设置映射请求请求路径的具体信息
① 更改 RequestMappingController 类的内容,如下
@Controller
@RequestMapping("/test")
public class RequestMappingController {
// 此时请求映射所映射的请求的请求路径为:/test/testRequestMapping
@RequestMapping("/testRequestMapping")
public String success(){
return "success";
}
}
② 在 index.html 中添加超链接
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>首页</h1>
<a th:href="@{/test/testRequestMapping}">测试RequestMapping注解的位置</a>
</body>
</html>
③ 在 src/main/webapp/WEB-INF/templates 目录下新建 success.html 页面用来测试
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
这是success页面
</body>
</html>
4、@RequestMapping 注解的 value 属性
打开 @RequestMapping 注解的源码,按下快捷键 alt+& 查看结构
@RequestMapping注解的value属性通过请求的请求地址匹配请求映射
@RequestMapping注解的value属性是一个字符串类型的数组,表示该请求映射能够匹配多个请求地址所对应的请求
@RequestMapping注解的value属性必须设置,至少通过请求地址匹配请求映射
① 更改 RequestMappingController 类的内容,如下:
@Controller
@RequestMapping("/test")
public class RequestMappingController {
// 此时请求映射所映射的请求的请求路径为:/test/testRequestMapping
@RequestMapping(
value = {"/testRequestMapping", "/hello"}
)
public String success(){
return "success";
}
}
② 在 index.html 页面添加超链接
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>首页</h1>
<a th:href="@{/test/testRequestMapping}">测试RequestMapping注解的位置</a><br>
<a th:href="@{/test/testRequestMapping}">测试RequestMapping注解的value属性 —— /test/testRequestMapping</a><br>
<a th:href="@{/test/hello}">测试RequestMapping注解的value属性 —— /test/hello</a><br>
</body>
</html>
③ success.html 页面的内容如下,用作测试
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
这是success页面
</body>
</html>
5、@RequestMapping 注解的 method 属性
@RequestMapping注解的method属性通过请求的请求方式(get或post)匹配请求映射
@RequestMapping注解的method属性是一个RequestMethod类型的数组,表示该请求映射能够匹配多种请求方式的请求
若当前请求的请求地址满足请求映射的value属性,但是请求方式不满足method属性,则浏览器报错405:Request method 'POST' not supported,如下图:
注:
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部分会讲到
① 在 RequestMappingController 修改内容如下:
@Controller
@RequestMapping("/test")
public class RequestMappingController {
// 此时请求映射所映射的请求的请求路径为:/test/testRequestMapping
@RequestMapping(
value = {"/testRequestMapping", "/hello"},
method = {RequestMethod.GET, RequestMethod.POST}
)
public String success(){
return "success";
}
// 测试GetMapping注解
@GetMapping("/testGetMapping")
public String testGetMapping(){
return "success";
}
// 测试form表单是否能够发送put或delete请求方式,结果:页面报405错误
@RequestMapping(value = "/testPut", method = RequestMethod.PUT)
public String testPut(){
return "success";
}
}
② 在 index.html 添加测试链接及表单
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>首页</h1><br>
<!-- 测试RequestMapping注解不同位置的作用 -->
<a th:href="@{/test/testRequestMapping}">测试RequestMapping注解的位置</a><br>
<!-- 试RequestMapping注解的value属性 -->
<a th:href="@{/test/testRequestMapping}">测试RequestMapping注解的value属性 —— /test/testRequestMapping</a><br>
<a th:href="@{/test/hello}">测试RequestMapping注解的value属性 —— /test/hello</a><br>
<!-- 测试RequestMapping注解的method属性 -->
<a th:href="@{/test/hello}">测试RequestMapping注解的method属性 —— GET</a><br>
<form th:action="@{/test/hello}" method="post">
<input type="submit" value="测试RequestMapping注解的method属性 —— POST">
</form>
<!-- 测试GetMapping -->
<a th:href="@{/test/testGetMapping}">测试GetMapping注解 —— /testGetMapping</a><br>
<!-- 测试form表单是否能够发送put或delete请求方式 -->
<form th:action="@{/test/testPut}" method="put">
<input type="submit" value="测试form表单是否能够发送put或delete请求方式">
</form>
</body>
</html>
③ success.html 页面
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
这是success页面
</body>
</html>
注:
测试form表单是否能够发送put或delete请求方式时,页面报405错误,如下:
6、@RequestMapping注解的params属性(了解)
@RequestMapping注解的params属性通过请求的请求参数匹配请求映射
@RequestMapping注解的params属性是一个字符串类型的数组,可以通过四种表达式设置请求参数和请求映射的匹配关系
"param":要求请求映射所匹配的请求必须携带param请求参数,报错情况如下:
"!param":要求请求映射所匹配的请求必须不能携带param请求参数,报错情况如下:
"param=value":要求请求映射所匹配的请求必须携带param请求参数且param=value,报错情况如下:
"param!=value":要求请求映射所匹配的请求必须携带param请求参数但是param!=value
① RequestMappingController 类添加方法
// 测试@RequestMapping注解的params属性
@RequestMapping(
value = "/testParamsAndHeaders",
params = {"username", "password!=123456"}
)
public String testParamsAndHeaders(){
return "success";
}
② index.html 中添加测试的超链接
<!-- 测试@RequestMapping注解的params属性 -->
<a th:href="@{/test/testParamsAndHeaders(username='admin',password=123456)}">测试@RequestMapping注解的params属性</a>
7、@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错误,即资源未找到
① 在 RequestMappingController 类中修改方法:
// 测试@RequestMapping注解的params属性
@RequestMapping(
value = "/testParamsAndHeaders",
params = {"username", "password!=123456"},
headers = {"Hostttt"}
)
public String testParamsAndHeaders(){
return "success";
}
② index.html 中添加测试的超链接
<!-- 测试@RequestMapping注解的params属性和header属性 -->
<a th:href="@{/test/testParamsAndHeaders(username='admin',password=123)}">测试@RequestMapping注解的params属性和header属性</a>
8、SpringMVC支持ant风格的路径(模糊匹配)
?:表示任意的单个字符(除了 ? \ #),空格也可以
* :表示任意的0个或多个字符(除了 ? \ #)
** :表示任意的一层或多层目录
注意:在使用**时,只能使用/**/xxx的方式(/** 单独作为一层目录),不能是 /a**a
① 在 RequestMappingController 类中添加方法
// 测试SpringMVC支持ant风格的路径
//@RequestMapping("/a?a/testAnt")
//@RequestMapping("/a*a/testAnt")
@RequestMapping("/**/testAnt")
public String testAnt(){
return "success";
}
② 在 index.html 中添加测试的超链接
<!-- 测试SpringMVC支持ant风格的路径 -->
<a th:href="@{/test/a1a/testAnt}">测试SpringMVC支持ant风格的路径 —— 使用 ?</a><br>
<a th:href="@{/test/a1a/testAnt}">测试SpringMVC支持ant风格的路径 —— 使用 *</a><br>
<a th:href="@{/test/a1a/testAnt}">测试SpringMVC支持ant风格的路径 —— 使用 **</a><br>
9、SpringMVC支持路径中的占位符(重点)
原始方式:/deleteUser?id=1
rest方式:/deleteUser/1
SpringMVC路径中的占位符常用于RESTful风格中,当请求路径中将某些数据通过路径的方式传输到服务器中,就可以在相应的@RequestMapping注解的value属性中通过占位符{xxx}表示传输的数据,在通过@PathVariable注解,将占位符所表示的数据赋值给控制器方法的形参
① 在 RequestMappingController 类添加方法
// SpringMVC支持路径中的占位符
// {} 就是占位符,里面的值是名字
@RequestMapping("/testPath/{id}/{username}")
// @PathVariable注解将 {id} 对应的值赋值给参数id
public String testPath(@PathVariable("id")Integer id, @PathVariable("username")String username){
System.out.println("id = " + id + ", username = " + username);
return "success";
}
② 在 index.html 中添加用于测试的超链接
<!-- 测试SpringMVC支持路径中的占位符 -->
<a th:href="@{/test/testPath/1/admin}">测试SpringMVC支持路径中的占位符</a><br>