目录
@RequestMapping注解的位置放置不同的位置也有不同的功能
@RequestMapping的成员参数value、method、params、headers的作用
当请求参数有重复,且请求控制器的方法的形参也是非数组而是String类型
@RequestParam三个属性value、required、defaultValue的介绍
(补充)通过CharacterEncodingFilter解决获取请求参数的乱码问题
@RequestMapping注解
说明:框架是由配置文件+jar包组成的
(前置工作)搭建springMVC-demo2模块(Maven工程+web模块)
1.创建新的Maven工程模块
2.在Pom.xml中引入依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.atguigu.mvc</groupId>
<artifactId>springMVC-demo2</artifactId>
<version>1.0-SNAPSHOT</version>
<!--工程打包方式,默认是jar,如果这是一个web工程,打包方式为war
当前是一个web工程,因此我们需要为这个Maven工程添加web模块,
模块里面有webapp及webapp里面的web.xml-->
<packaging>war</packaging>
<!--添加依赖群,当导报成war包时,这些依赖都会被放到webapp目录的
WEB-INF目录中的lib目录下-->
<dependencies>
<!--添加单个依赖SpringMVC,下面同理.由于 Maven 的传递性,
我们不必将所有需要的包全部配置依赖,而是配置最顶端的依赖,其他靠传递性导入,
意味着只要导入了这个SpringMVC依赖,那么
SpringMVC依赖的依赖也会导入进来,其中就有spring的基础框架jar包:
aop、beans、context、core、expression,还有自身web框架jar包
传递性对于其他依赖同理-->
<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,之前没导入是因为Tomcat自带了Servlet和JSP的jar包,
因此只要配置了tomcat就能使用Servlet,java不自带Servlet的-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<!--依赖范围,表示我们什么地方可以使用这个jar包,
这里设置成provided就是该依赖已被服务器提供,因此编译时
这个依赖都会被放到webapp目录的WEB-INF目录中的lib目录下-->
<scope>provided</scope>
</dependency>
<!-- Spring5和Thymeleaf整合包,是视图技术,控制页面显示内容 -->
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring5</artifactId>
<version>3.0.12.RELEASE</version>
</dependency>
</dependencies>
</project>
3.刷新Maven,为项目配置依赖
4.为Maven项目配置Web模块
5.在web.xml中注册SpringMVC的前端控制器DispatcherServlet
<?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">
<!-- 配置SpringMVC的前端控制器DispatcherServlet,对浏览器发送的请求统一进行处理 -->
<servlet>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--通过初始化参数(属性)contextConfigLocation配置Spring配置文件的位置和名称,
该属性随着DispatcherServlet初始化而初始化,
是DispatcherServlet是DispatcherServlet定义好的-->
<init-param>
<!--属性名 = contextConfigLocation,如果忘了可以参考一下:
http://t.csdn.cn/UuVZl这个文章-->
<param-name>contextConfigLocation</param-name>
<!--属性值 = 配置文件路径
classpath:对应的是编译后的java目录和resources目录的路径-->
<param-value>classpath:springMVC.xml</param-value>
</init-param>
<!--设置该Servlet的生命周期,该前端控制器需要在程序启动时启动,来确保程序的性能,
而不是第一次访问该DispatcherServlet时候才初始化,
因此设置成该servlet的参数load-on-startup值为1-->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<!--保持servlet和servlet-mapping的servlet名字一致,表示要注册的Servlet是DispatcherServlet-->
<servlet-name>DispatcherServlet</servlet-name>
<!--
设置springMVC的核心的前端控制器所能处理的请求的请求路径,
这里"/"所匹配的请求可以是表示该Servlet可以处理除了.jsp以外的请求,
.jsp文件在Tomcat中有专门名为"jspServlet"去处理
当我们设置了前端控制器的请求映射规则为"/"时,
那么原有的Tomcat的默认Servlet就会被该前端控制器取代
-->
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
6.在resources目录下创建SpringMVC的配置文件,添加IOC组件扫描及Thymeleaf视图解析器
<?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">
<!--添加扫描组件,让spring注解生效-->
<context:component-scan base-package="com.atguigu.mvc.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-->
<bean class="org.thymeleaf.spring5.SpringTemplateEngine">
<!--内部Bean中的内部Bean-->
<property name="templateResolver">
<bean class="org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver">
<!-- 视图前缀 -->
<property name="prefix" value="/WEB-INF/templates/"/>
<!-- 视图后缀,设置好视图前缀和后缀,
当后端请求控制器的方法返回"index"字符串时,
访问的就是服务器src\main\webapp\WEB-INF\templates\index.html页面-->
<property name="suffix" value=".html"/>
<!--设置模板模型-->
<property name="templateMode" value="HTML5"/>
<!--页面编码-->
<property name="characterEncoding" value="UTF-8" />
</bean>
</property>
</bean>
</property>
</bean>
</beans>
7.在 src\main\java\com\atguigu\mvc\controller\TestController.java 创建请求控制器,内容如下
package com.atguigu.mvc.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
/*
该注解标识该类为IOC容器的控制层组件,通过包扫描讲这个类变成Bean交给Spring IOC容器管理,
SpringMVC才知道这里面的方法是处理控制器接收的请求,并作出响应.此时该类就变成了一个请求控制器
*/
@Controller
public class TestController {
/**
* 目的:当浏览器访问"http://域名:端口/程序上下文路径"时,
* 应该是访问服务器的"src\main\webapp\WEB-INF\templates\index.html"页面
* 方法名是无所谓的,因为主要还是通过该方法的方法体进行业务处理,
* RequestMapping是一个请求映射注解,
* value参数成员的值是对应请求资源路径(只用一个参数成员value可以不写),
* 二者结合用于匹配访问服务器资源路径仅带有"/"的请求与请求控制器对应的方法
* @return 返回值返回的是视图名称,是请求控制器返回给前端控制器,
* 告知前端控制器的视图解析器解析要执行哪一个页面
*/
@RequestMapping(value = "/")
public String index(){
return "index";
}
}
8.创建通过视图解析器解析视图名称,得到的视图src\main\webapp\WEB-INF\templates\index.html,页面还要通过Thymeleaf视图渲染器进行渲染,内容如下
<!DOCTYPE html>
<!--添加一个Thymeleaf的命名空间-->
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>首页</title>
</head>
<body>
<h1>首页</h1>
</body>
</html>
9.修改Tomcat配置,配置springMVC-demo2模块,也可以再创建一个