3、SpringMVC之RequestMapping注解
3.1、环境搭建创建名为spring_mvc_demo的新module,过程参考2.1节
3.1.1、创建SpringMVC的配置文件
https://img2023.cnblogs.com/blog/2052479/202309/2052479-20230921083229254-198496711.png
<?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="org.rain.controller"></context:component-scan>
<bean id="viewResolver" >
<property name="order" value="1"/>
<property name="characterEncoding" value="UTF-8"/>
<property name="templateEngine">
<bean >
<property name="templateResolver">
<bean
>
<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>3.1.2、配置web.xml
https://img2023.cnblogs.com/blog/2052479/202309/2052479-20230921084338842-245988243.png
<servlet>
<servlet-name>SpringMVC</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>SpringMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>3.1.3、创建请求控制器
https://img2023.cnblogs.com/blog/2052479/202309/2052479-20230921084826828-683007276.png
package org.rain.controller;
import org.springframework.stereotype.Controller;
/**
* @author liaojy
* @date 2023/9/21 - 8:47
*/
@Controller
public class TestRequestMappingController {
}https://img2023.cnblogs.com/blog/2052479/202309/2052479-20230921085139876-147794038.png
package org.rain.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* @author liaojy
* @date 2023/9/21 - 8:49
*/
@Controller
public class PortalController {
@RequestMapping("/")
public String portal(){
return "index";
}
}3.1.4、创建静态资源目录及页面
https://img2023.cnblogs.com/blog/2052479/202309/2052479-20230921085452504-870645390.png
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>首页</title>
</head>
<body>
<h1>index.html</h1>
</body>
</html>https://img2023.cnblogs.com/blog/2052479/202309/2052479-20230921091842542-208519241.png
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>成功</title>
</head>
<body>
<h1>success.html</h1>
</body>
</html>3.1.5、配置tomcat
https://img2023.cnblogs.com/blog/2052479/202309/2052479-20230921090119935-146261411.png
https://img2023.cnblogs.com/blog/2052479/202309/2052479-20230921090144535-405406960.png
细节请参考2.6节
3.2、注解的功能
[*]从注解名称上可以看出,@RequestMapping注解的作用就是将请求和处理请求的控制器方法关联起来,建立映射关系;
[*]SpringMVC 的前端控制器(DispatcherServlet)接收到请求后,就会在映射关系中找到对应的控制器方法来处理这个请求;
3.3、注解的位置
3.3.1、源码定义
https://img2023.cnblogs.com/blog/2052479/202309/2052479-20230921091209884-1918772635.png
从源码可知,@RequestMapping注解既可以标识在类上,也可以标识在方法上
3.3.2、控制器示例
[*]@RequestMapping标识在类上:设置映射请求的基础信息
[*]@RequestMapping标识在方法上:设置映射请求的具体信息
https://img2023.cnblogs.com/blog/2052479/202309/2052479-20230921094742828-2068249306.png
@Controller
@RequestMapping("/test")
public class TestRequestMappingController {
// 此时控制器方法所匹配的请求的请求路径为:/test/hello
@RequestMapping("/hello")
public String hello(){
return "success";
}
}3.3.3、请求示例
https://img2023.cnblogs.com/blog/2052479/202309/2052479-20230921095242044-420790328.png
注意html要引入thymeleaf的约束:xmlns:th="http://www.thymeleaf.org"
<a th:target="_blank" href="https://www.cnblogs.com/@{/hello}">测试/hello请求</a>
<a th:target="_blank" href="https://www.cnblogs.com/@{/test/hello}">测试/test/hello请求</a>3.3.4、测试效果
https://img2023.cnblogs.com/blog/2052479/202309/2052479-20230921095602651-1355663442.png
https://img2023.cnblogs.com/blog/2052479/202309/2052479-20230921095749497-621207855.png
https://img2023.cnblogs.com/blog/2052479/202309/2052479-20230921095659372-865612547.png
https://img2023.cnblogs.com/blog/2052479/202309/2052479-20230921095729138-308663103.png
3.3.5、双重位置的作用
可以根据业务需要划分模块,在请求路径中就可以体现出请求的是哪个模块的资源
3.4、注解的value属性
@RequestMapping注解的value属性必须设置,其作用是根据请求路径来匹配请求
3.4.1、源码定义
https://img2023.cnblogs.com/blog/2052479/202309/2052479-20230922083436901-1192451056.png
value属性的别名是path,所以用path属性代替也可以;
value属性是字符串数组类型,所以可以设置多个值;
3.4.2、匹配多个请求的控制器示例
https://img2023.cnblogs.com/blog/2052479/202309/2052479-20230922084713957-20271376.png
普通的servlet,也可以在web.xml的标签中,设置多个子标签,从而实现同样的效果
@RequestMapping({"/hello","/hi"})https://img2023.cnblogs.com/blog/2052479/202309/2052479-20230922085422477-1799366532.png
<a th:target="_blank" href="https://www.cnblogs.com/@{/test/hi}">测试/test/hi请求</a>3.4.3、测试效果
https://img2023.cnblogs.com/blog/2052479/202309/2052479-20230922085556121-1427714369.png
https://img2023.cnblogs.com/blog/2052479/202309/2052479-20230922085859787-574251545.png
https://img2023.cnblogs.com/blog/2052479/202309/2052479-20230922085759059-446250019.png
https://img2023.cnblogs.com/blog/2052479/202309/2052479-20230922085840984-779122660.png
3.5、注解的method属性
[*]@RequestMapping注解的method属性的作用是,根据请求方式(get或post)匹配请求
[*]若当前请求的请求地址满足value属性,但是请求方式不满足method属性,
则浏览器报错 405:Request method '请求方式' not supported
3.5.1、源码定义
https://img2023.cnblogs.com/blog/2052479/202309/2052479-20230922092155938-905116490.png
method属性是RequestMethod数组类型,所以可以设置多个值;
https://img2023.cnblogs.com/blog/2052479/202309/2052479-20230922092321769-442659207.png
RequestMethod是枚举类型,有固定的可选值
3.5.2、控制器示例
https://img2023.cnblogs.com/blog/2052479/202309/2052479-20230923100026470-1628467791.png
@RequestMapping(value = {"/hello","/hi"},method = {RequestMethod.GET,RequestMethod.POST})3.5.3、请求示例
https://img2023.cnblogs.com/blog/2052479/202309/2052479-20230923102456753-229920819.png
<a th:target="_blank" href="https://www.cnblogs.com/@{/test/hello}">测试/test/hello请求</a>
<form th:action="@{/test/hello}" method="post">
<input type="submit" value="测试@RequestMapping注解的method属性的post请求">
</form>3.5.4、测试效果
https://img2023.cnblogs.com/blog/2052479/202309/2052479-20230923101749880-1596885760.png
https://img2023.cnblogs.com/blog/2052479/202309/2052479-20230923101655148-1865200678.png
++++++++++++++++++++++++++分割线++++++++++++++++++++++++++
https://img2023.cnblogs.com/blog/2052479/202309/2052479-20230923101733915-761834127.png
https://img2023.cnblogs.com/blog/2052479/202309/2052479-20230923102356874-1038206919.png
3.5.5、结合请求方式的派生注解
对于处理指定请求方式的控制器方法,SpringMVC中提供了@RequestMapping的派生注解
[*]处理get请求的派生注解-->@GetMapping
[*]处理post请求的派生注解-->@PostMapping
[*]处理put请求的派生注解-->@PutMapping
[*]处理delete请求的派生注解-->@DeleteMapping
https://img2023.cnblogs.com/blog/2052479/202309/2052479-20230923105304783-570739434.png
// 只处理post请求方式的请求
@PostMapping("/hello")https://img2023.cnblogs.com/blog/2052479/202309/2052479-20230923105350665-1001959454.png
https://img2023.cnblogs.com/blog/2052479/202309/2052479-20230923105440503-1305034397.png
++++++++++++++++++++++++++分割线++++++++++++++++++++++++++
https://img2023.cnblogs.com/blog/2052479/202309/2052479-20230923105511138-712298893.png
https://img2023.cnblogs.com/blog/2052479/202309/2052479-20230923105521310-330724892.png
3.6、注解的params属性(了解)
[*]@RequestMapping注解的params属性的作用是,根据请求参数匹配请求
[*]浏览器发送的请求的请求参数,必须满足params属性的设置(如果有的话)
否则报错:HTTP状态 400 - 错误的请求
3.6.1、源码定义
https://img2023.cnblogs.com/blog/2052479/202309/2052479-20230925090029502-1629885939.png
params属性是字符串数组类型,所以可以设置多个值;
注意:请求必须满足params属性所有值的要求。
3.6.2、params属性的四种表达式
[*]"param":表示所匹配的请求的请求参数中,必须携带param参数
[*]"!param":表示所匹配的请求的请求参数中,必须不能携带param参数
[*]"param=value":表示所匹配的请求的请求参数中,必须携带param参数,且值必须为value
[*]"param!=value":表示所匹配的请求的请求参数中,可以不携带param参数;若携带param参数,其值必须不能为value
3.6.3、控制器示例
https://img2023.cnblogs.com/blog/2052479/202309/2052479-20230925144537567-1629601587.png
@RequestMapping(
value = {"/hello","/hi"},
method = {RequestMethod.GET,RequestMethod.POST},
params = {"username","!password","age=18","gender!=女"}
)3.6.4、请求示例
https://img2023.cnblogs.com/blog/2052479/202309/2052479-20230925144712235-1019248651.png
<a th:target="_blank" href="https://www.cnblogs.com/@{/test/hello?username=admin&age=18}">测试@RequestMapping注解的params属性(传统?传参)</a>
<br><br>
<a th:target="_blank" href="https://www.cnblogs.com/@{/test/hello(username='admin',age=18)}">测试@RequestMapping注解的params属性(thymeleaf语法传参)</a>3.6.5、测试效果
https://img2023.cnblogs.com/blog/2052479/202309/2052479-20230925144835040-1811100787.png
https://img2023.cnblogs.com/blog/2052479/202309/2052479-20230925144953375-1090167224.png
++++++++++++++++++++++++++分割线++++++++++++++++++++++++++
https://img2023.cnblogs.com/blog/2052479/202309/2052479-20230925145042013-2091001367.png
https://img2023.cnblogs.com/blog/2052479/202309/2052479-20230925145107025-185369391.png
++++++++++++++++++++++++++分割线++++++++++++++++++++++++++
https://img2023.cnblogs.com/blog/2052479/202309/2052479-20230925145147782-1573354355.png
https://img2023.cnblogs.com/blog/2052479/202309/2052479-20230925145252159-1110444007.png
3.7、注解的headers属性(了解)
[*]@RequestMapping注解的headers属性的作用是,根据请求头信息匹配请求
[*]浏览器发送的请求的请求头信息,必须满足headers属性的设置(如果有的话)
否则报错:HTTP状态 404 - 未找到
3.7.1、源码定义
https://img2023.cnblogs.com/blog/2052479/202309/2052479-20230925152321850-1001760811.png
headers属性是字符串数组类型,所以可以设置多个值;
注意:请求必须满足headers属性所有值的要求。
3.7.2、headers属性的四种表达式
[*]"header":表示所匹配的请求,必须携带header请求头信息
[*]"!header":表示所匹配的请求,必须不能携带header请求头信息
[*]"header=value":表示所匹配的请求,必须携带header请求头信息,且值必须为value
[*]"header!=value":表示所匹配的请求,可以不携带header请求头信息;若携带header请求头信息,其值必须不能为value
3.7.3、控制器示例
https://img2023.cnblogs.com/blog/2052479/202309/2052479-20230925152937477-78427628.png
注意:请求头信息的键不区分大小写,但其值区分大小写
@RequestMapping(
value = {"/hello","/hi"},
method = {RequestMethod.GET,RequestMethod.POST},
params = {"username","!password","age=18","gender!=女"},
headers = {"referer"}
)3.7.4、请求示例
https://img2023.cnblogs.com/blog/2052479/202309/2052479-20230925153138743-1555738299.png
https://img2023.cnblogs.com/blog/2052479/202309/2052479-20230925153244373-1869892327.png
Referer请求头表示请求的来源,本例的请求来源为http://localhost:8080/spring_mvc_demo/
https://img2023.cnblogs.com/blog/2052479/202309/2052479-20230925153619930-773447479.png
如果是通过浏览器地址栏直接访问,是没有Referer(请求来源)的
3.8、ant风格的路径
3.8.1、通配符
[*]? :表示任意的单个字符(不包括 ? 和 / ,因为它们是分隔符)
[*]* :表示任意个数的任意字符(不包括 ? 和 / )
[*]** :表示任意层数的任意目录(其用法是 /**/xxx 的方式)
3.8.2、?通配符示例
https://img2023.cnblogs.com/blog/2052479/202309/2052479-20230926100404086-1093790355.png
@RequestMapping("/a?a")
public String testAnt(){
return "success";
}https://img2023.cnblogs.com/blog/2052479/202309/2052479-20230926100457849-111356688.png
https://img2023.cnblogs.com/blog/2052479/202309/2052479-20230926100536582-1914783735.png
https://img2023.cnblogs.com/blog/2052479/202309/2052479-20230926101000849-316257144.png
3.8.3、*通配符示例
https://img2023.cnblogs.com/blog/2052479/202309/2052479-20230926101719853-825900093.png
@RequestMapping("/a*a")
public String testAnt(){
return "success";
}https://img2023.cnblogs.com/blog/2052479/202309/2052479-20230926101752244-1328047672.png
https://img2023.cnblogs.com/blog/2052479/202309/2052479-20230926101817515-953827041.png
https://img2023.cnblogs.com/blog/2052479/202309/2052479-20230926102638472-858868767.png
3.8.4、** 通配符示例
https://img2023.cnblogs.com/blog/2052479/202309/2052479-20230926102220718-1004291278.png
@RequestMapping("/**/aa")
public String testAnt(){
return "success";
}https://img2023.cnblogs.com/blog/2052479/202309/2052479-20230926102252567-1263963400.png
https://img2023.cnblogs.com/blog/2052479/202309/2052479-20230926102315305-576706642.png
3.9、路径中的占位符(重点)
[*]原始风格的请求(?分隔符前面是路径,后面是参数):/deleteUser?id=1
[*]RESTful 风格的请求(参数是路径的一部分):/user/delete/1
[*]SpringMVC 路径中的占位符常用于RESTful风格中;
[*]在@RequestMapping注解的value属性中,通过占位符{xxx}表示路径中传输的数据;
[*]再通过@PathVariable注解,将占位符所表示的数据赋值给控制器方法的形参。
3.9.1、控制器示例
https://img2023.cnblogs.com/blog/2052479/202309/2052479-20230927101039516-519931434.png
对于占位符的数据类型,@PathVariable注解会自动转换赋值给控制器方法的形参
@RequestMapping("/rest/{restfulId}/{restfulUsername}")
public String testRestful(@PathVariable("restfulId") Integer id,@PathVariable("restfulUsername") String username){
System.out.println("id:"+id);
System.out.println("username:"+username);
return "success";
}3.9.2、请求示例
https://img2023.cnblogs.com/blog/2052479/202309/2052479-20230927101210235-2079270542.png
<a th:target="_blank" href="https://www.cnblogs.com/@{/test/rest/12345/zhangsan}">测试@RequestMapping注解的value属性中的占位符</a>3.9.3、测试效果
https://img2023.cnblogs.com/blog/2052479/202309/2052479-20230927101409019-2062244791.png
https://img2023.cnblogs.com/blog/2052479/202309/2052479-20230927101447142-213389424.png
https://img2023.cnblogs.com/blog/2052479/202309/2052479-20230927101514073-1282487235.png
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
页:
[1]