Spring5 | MVC基础

1.SpringMVC概述

  1. Spring的延续,主流JavaEE项目表述层开发的首选方案
  2. 三层架构分为表述层、业务逻辑层、数据访问层、表述层表示前台页面和后台servlet

1.1特点

  1. 与IOC容器无缝对接
  2. 底层基于原生Servlet,通过强大的DispatherServlet(前端控制器),对请求和响应进行统一处理
  3. 表述层各细分领域需要解决的问题全方位覆盖,提供全解决方案
  4. 可插拔式组件,需要使用进行配置即可

2.依赖准备

  1. SpringMVC项目基于Maven
  2. 除了IOC中提到的核心依赖外,还需要2.1的依赖
  3. Tomcat的配置根据9和10自行切换依赖jar包,此处搭建环境为Tomcat9
  4. 注:添加打包方式,避免报500或404错误

2.1Maven依赖

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<packaging>war</packaging>

<!--logback日志-->
<!-- https://mvnrepository.com/artifact/ch.qos.logback/logback-classic -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</dependency>

<!--Tomcat9 servlet api-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>

<!--Spring5和thymeleaf整合包-->
<!-- https://mvnrepository.com/artifact/org.thymeleaf/thymeleaf-spring5 -->
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring5</artifactId>
<version>3.0.12.RELEASE</version>
</dependency>

2.2web.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<!--在WEB_INF中的web.xml添加前端控制器dispatcherServlet用于对浏览器发送的请求统一处理-->
<servlet>
<servlet-name>SpringMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--配置SpringMVC配置文件的位置和名称-->
<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>
<!-- / 代表当前浏览器发送的所用请求,但不包括jsp-->
<url-pattern>/</url-pattern>
</servlet-mapping>

2.3Bean.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<!--1.注解扫描组件-->
<context:component-scan base-package="被扫描包路径"/>
<!--2.配置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>

3.测试代码

3.1HTML页面

主页面

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<!--根据Bean文件中配置的thymeleaf视图,创建templates文件夹-->
<!DOCTYPE html>
<!--通过xml解析引入thymeleaf视图-->
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>hello world</h1>
<!--调用thymeleaf通过xml解析配合Bean文件实现页面跳转-->
<a th:href="@{/target}">target页面</a>
</body>
</html>

target页面

1
2
3
4
5
6
7
8
9
10
11
<!DOCTYPE html>
<!--跳转到的页面-->
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>target a target</h1>
</body>
</html>

3.2Controller类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
@Controller
public class HelloController {
// "/"-->WEB-INFO/templates/index.html

// 通过springMVC配置文件中的thymeleaf视图来读取中间信息
@RequestMapping(value = "/") //为这个方法创建映射关系
public String indexMethod(){
//返回视图名称
return "index";
}

@RequestMapping(value = "/target")
public String toTarget(){
return "target";
}
}

Spring5 | MVC基础
http://example.com/2022/10/21/SpringMVC/SpringMVC基础/
Author
John Doe
Posted on
October 21, 2022
Licensed under