三、SpringBoot整合Thymeleaf视图
3.1 Thymeleaf视图介绍
先看下官网的介绍:
Thymeleaf是适用于Web和独立环境的现代服务器端Java模板引擎。
Thymeleaf的主要目标是为您的开发工作流程带来优雅的自然模板 -HTML可以在浏览器中正确显示,也可以作为静态原型工作,从而可以在开发团队中加强协作。
Thymeleaf拥有适用于Spring Framework的模块,与您喜欢的工具的大量集成以及插入您自己的功能的能力,对于现代HTML5 JVM Web开发而言,Thymeleaf是理想的选择。
在SpringBoot中,SpringBoot对Thymeleaf提供了良好的支持,同时也提供了自动化配置,因此在SpringBoot中使用Thymeleaf非常快捷方便。
3.2 创建SpringBoot项目
创建方法建议使用IDEA快速创建SpringBoot项目,并选择web、Thymeleaf依赖:

创建完成后,IDEA自动在pom中加入了web和Thymeleaf依赖管理,pom.xml:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
项目架构:

3.2 配置Thymeleaf
SpringBoot为Thymeleaf提供了自动化配置类ThymeleafAutoConfiguration,源码:
@Configuration
@EnableConfigurationProperties({ThymeleafProperties.class})
@ConditionalOnClass({TemplateMode.class, SpringTemplateEngine.class})
@AutoConfigureAfter({WebMvcAutoConfiguration.class, WebFluxAutoConfiguration.class})
public class ThymeleafAutoConfiguration {...}
可以看出相关的配置信息是从ThymeleafProperties类中获得的,进一步查看ThymeleafProperties的源码:
@ConfigurationProperties(
prefix = "spring.thymeleaf"
)
public class ThymeleafProperties {
private static final Charset DEFAULT_ENCODING;
public static final String DEFAULT_PREFIX = "classpath:/templates/";
public static final String DEFAULT_SUFFIX = ".html";
private boolean checkTemplate = true;
private boolean checkTemplateLocation = true;
private String prefix = "classpath:/templates/";
private String suffix = ".html";
private String mode = "HTML";
//省略
}
从该配置可以看出默认的Thymeleaf存放位置是classpath:/templates/,即resources/templates/下,刚刚我们使用IDEA创建项目时,已经自动生成了该目录。
我们如果需要对Thymeleaf的配置进行更改,可直接在application.properties中配置:
#是否开启缓存,默认为true
spring.thymeleaf.cache=false
#检查模板文件是否存在
spring.thymeleaf.check-template=true
#检查模本目录是否存在
spring.thymeleaf.check-template-location=true
#模板文件编码
spring.thymeleaf.encoding=UTF-8
#模板位置
spring.thymeleaf.prefix=classpath:/templates/
#模板文件后缀名
spring.thymeleaf.suffix=.html
#Content-type
spring.thymeleaf.servlet.content-type=text/html
3.3 编写Demo
1、新建User和UserController:
User.java:
package com.gongsir.springboot02.pojo;
public class User {
private String name;
private String major;
private String grade;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getMajor() {
return major;
}
public void setMajor(String major) {
this.major = major;
}
public String getGrade() {
return grade;
}
public void setGrade(String grade) {
this.grade = grade;
}
}
UserController.java:
@Controller
public class UserController {
@GetMapping(path = "/users")
public ModelAndView getUsers(){
List<User> list = new ArrayList<>();
User u1 = new User();
u1.setName("龚涛");
u1.setMajor("计算机");
u1.setGrade("2017");
list.add(u1);
User u2 = new User();
u2.setName("李诗雅");
u2.setMajor("网络工程");
u2.setGrade("2017");
list.add(u2);
//视图模板文件的名字,需在template目录下创建同名模板文件
ModelAndView mv = new ModelAndView("users");
mv.addObject("users",list);
return mv;
}
}
2、在模板目录下新建users.html模板文件,显示数据:
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>用户列表</title>
</head>
<body>
<table border="1px sold black">
<tr>
<td>姓名</td>
<td>专业</td>
<td>年级</td>
</tr>
<tr th:each="user:${users}">
<td th:text="${user.name}"></td>
<td th:text="${user.major}"></td>
<td th:text="${user.grade}"></td>
</tr>
</table>
</body>
</html>
3、启动项目,访问http://localhost:8080/users,如图:

3.4 小结
本文主要介绍SpringBoot整合Thymeleaf视图技术,并给了一个简单demo演示,想学习更多Thymeleaf知识?看官网吧:https://www.thymeleaf.org/.
不过当前流行前后端分离技术,大多数开发不需要在后端整合视图技术,后端只需要提供接口即可,待续.....
三、SpringBoot整合Thymeleaf视图的更多相关文章
- Springboot整合thymeleaf模板
Thymeleaf是个XML/XHTML/HTML5模板引擎,可以用于Web与非Web应用. Thymeleaf的主要目标在于提供一种可被浏览器正确显示的.格式良好的模板创建方式,因此也可以用作静态建 ...
- SpringBoot 整合 Thymeleaf & 如何使用后台模板快速搭建项目
如果你和我一样,是一名 Java 道路上的编程男孩,其实我不太建议你花时间学 Thymeleaf,当然他的思想还是值得借鉴的.但是他的本质在我看来就是 Jsp 技术的翻版(Jsp 现在用的真的很少很少 ...
- 【Java Web开发学习】Spring4整合thymeleaf视图解析
[Java Web开发学习]Spring4整合thymeleaf视图解析 目录 1.简单介绍2.简单例子 转载:https://www.cnblogs.com/yangchongxing/p/9111 ...
- 【Springboot】Springboot整合Thymeleaf模板引擎
Thymeleaf Thymeleaf是跟Velocity.FreeMarker类似的模板引擎,它可以完全替代JSP,相较与其他的模板引擎,它主要有以下几个特点: 1. Thymeleaf在有网络和无 ...
- SpringBoot学习9:springboot整合thymeleaf
1.创建maven项目,添加项目所需依赖 <!--springboot项目依赖的父项目--> <parent> <groupId>org.springframewo ...
- SpringBoot 整合thymeleaf
1.Thymeleaf介绍(官网推荐:https://www.thymeleaf.org/doc/articles/thymeleaf3migration.html) Thymeleaf是跟Veloc ...
- SpringBoot:2.SpringBoot整合Thymeleaf模板引擎渲染web视图
在Web开发过程中,Spring Boot可以通过@RestController来返回json数据,那如何渲染Web页面?Spring Boot提供了多种默认渲染html的模板引擎,主要有以下几种: ...
- springboot整合Thymeleaf模板引擎
引入依赖 需要引入Spring Boot的Thymeleaf启动器依赖. <dependency> <groupId>org.springframework.boot</ ...
- springboot整合thymeleaf+tiles示例
网上关于此框架的配置实在不多,因此想记录下来以防忘记 因为公司框架基于上述(公司采用gradle构建项目,楼主采用的是maven),所以楼主能少走些弯路: 1.创建springboot-maven项目 ...
随机推荐
- python 当前时间多加一天、一小时、一分钟(转载)
首先看下,datetime的使用 import datetime >>> print datetime.datetime.now() 2017-07-15 15:01:24.6190 ...
- 部署django到服务器
部署 服务器环境配置 在本地的虚拟环境中,项目根目录下,执行命令收集所有的包 pip freeze > plist.txt 安装并创建虚拟环境,如已创建则跳过此步 sudo apt-get in ...
- VMware Tools安装后设置自动挂载解决共享文件夹无法显示的问题
一. 确保成功安装了VMware Tools 二. 使用如下命令 1.apt-get install open-vm-tools 2.vmhgfs-fuse .host:/ /mnt/hgfs ...
- 【搜索】$P1092$虫食算
题目链接 首先,我们只考虑加法的虫食算.这里的加法是N进制加法,算式中三个数都有N位,允许有前导的0. 其次,虫子把所有的数都啃光了,我们只知道哪些数字是相同的,我们将相同的数字用相同的字母表示,不同 ...
- 选择排序之python实现
def findsmallestindex(arr): smallnum = arr[0] smallindex = 0 # 寻找最小元素的位置 for i in range(1,len(arr)): ...
- eclipseWeb项目如何实现网址发送给外人——内部穿透
教程:https://blog.csdn.net/Feihongxiansen/article/details/94480480 部署完成后: 打开cmd命令: 暴露端口8081成功: 将eclips ...
- K8S集群搭建——基于CentOS 7系统
环境准备集群数量此次使用3台CentOS 7系列机器,分别为7.3,7.4,7.5 节点名称 节点IPmaster 192.168.0.100node1 192.168.0.101node2 192. ...
- docker时区问题
解决: dockerfile: RUN cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime#update application timezoneR ...
- 【Beta】设计与计划
目录 新增功能描述 小程序:按模块说明需求 用户权限模块 新闻模块 活动模块 社团模块 社团管理模块 小程序:按页面说明需求变化 新闻页 活动页/活动列表页 活动详情页 社团页 某类别社团页 社团详情 ...
- qt5 源码编译
源码 qt-everywhere-src-5.11.3 依赖 apt-get install bison build-essential gperf flex ruby python libasoun ...