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视图的更多相关文章

  1. Springboot整合thymeleaf模板

    Thymeleaf是个XML/XHTML/HTML5模板引擎,可以用于Web与非Web应用. Thymeleaf的主要目标在于提供一种可被浏览器正确显示的.格式良好的模板创建方式,因此也可以用作静态建 ...

  2. SpringBoot 整合 Thymeleaf & 如何使用后台模板快速搭建项目

    如果你和我一样,是一名 Java 道路上的编程男孩,其实我不太建议你花时间学 Thymeleaf,当然他的思想还是值得借鉴的.但是他的本质在我看来就是 Jsp 技术的翻版(Jsp 现在用的真的很少很少 ...

  3. 【Java Web开发学习】Spring4整合thymeleaf视图解析

    [Java Web开发学习]Spring4整合thymeleaf视图解析 目录 1.简单介绍2.简单例子 转载:https://www.cnblogs.com/yangchongxing/p/9111 ...

  4. 【Springboot】Springboot整合Thymeleaf模板引擎

    Thymeleaf Thymeleaf是跟Velocity.FreeMarker类似的模板引擎,它可以完全替代JSP,相较与其他的模板引擎,它主要有以下几个特点: 1. Thymeleaf在有网络和无 ...

  5. SpringBoot学习9:springboot整合thymeleaf

    1.创建maven项目,添加项目所需依赖 <!--springboot项目依赖的父项目--> <parent> <groupId>org.springframewo ...

  6. SpringBoot 整合thymeleaf

    1.Thymeleaf介绍(官网推荐:https://www.thymeleaf.org/doc/articles/thymeleaf3migration.html) Thymeleaf是跟Veloc ...

  7. SpringBoot:2.SpringBoot整合Thymeleaf模板引擎渲染web视图

    在Web开发过程中,Spring Boot可以通过@RestController来返回json数据,那如何渲染Web页面?Spring Boot提供了多种默认渲染html的模板引擎,主要有以下几种: ...

  8. springboot整合Thymeleaf模板引擎

    引入依赖 需要引入Spring Boot的Thymeleaf启动器依赖. <dependency> <groupId>org.springframework.boot</ ...

  9. springboot整合thymeleaf+tiles示例

    网上关于此框架的配置实在不多,因此想记录下来以防忘记 因为公司框架基于上述(公司采用gradle构建项目,楼主采用的是maven),所以楼主能少走些弯路: 1.创建springboot-maven项目 ...

随机推荐

  1. cogs 943. [東方S3] 铃仙•优昙华院•稻叶

    二次联通门 : cogs 943. [東方S3] 铃仙•优昙华院•稻叶 /* cogs 943. [東方S3] 铃仙·优昙华院·稻叶 概率dp 貌似做麻烦了 邻接矩阵和链式前向星都用上了... dp[ ...

  2. uni-app 组件

    组件:组件时视图层的基本组成单元 <template> <view> <tagname property = "value"> content ...

  3. linux jar/war包 后台运行

    1. 基础版,当前ssh窗口锁定,按CTRL+C打断程序运行:或关闭窗口,程序退出 java -jar flowable-modeler.war 2. 改进版,当前ssh窗口不锁定,窗口关闭时,程序终 ...

  4. prometheus(docker)安装和报警 -- nginx域名监控

    软件组件:prometheusalertmanagerprometheus-webhook-dingtalk nginx-vts-exporternginx (###--add-module=../n ...

  5. [Gamma]Scrum Meeting#3

    github 本次会议项目由PM召开,时间为5月28日晚上10点30分 时长10分钟 任务表格 人员 昨日工作 下一步工作 木鬼 撰写博客,组织例会 撰写博客,组织例会 swoip 前端显示屏幕,翻译 ...

  6. IntelliJ IDEA 调试 Apache RocketMQ 源码

    克隆源码 mvn clean install 执行命令,跳过测试.我在执行测试的时候有时候卡住,所以干脆就跳过了. mvn clean install -DskipTests 准备环境 在 D 盘创建 ...

  7. eclipse无法访问sun.misc.Unsafe类的解决办法

    参考:https://www.cnblogs.com/duanxz/p/6090442.html

  8. Linux系统实现虚拟内存有两种方法:交换分区(swap分区)和交换文件

    Linux系统实现虚拟内存有两种方法:交换分区(swap分区)和交换文件 交换文件 查看内存:free -m , -m是显示单位为MB,-g单位GB 创建一个文件:touch /root/swapfi ...

  9. nestjs pm2 启动 静态文件404报错

    不要直接使用pm2 start 可执行文件,静态文件会显示404. 使用如下方式:

  10. springboot装配OkHttp组件

    在SpringBoot应用中,发送Http通常我们使用RestTemplate,但有部分组件底层是使用OkHttp进行Http的操作,而且OKHttp也是一个很优秀的HTTP组件. RestTempa ...