博主信息
博文 41
粉丝 0
评论 0
访问量 40800
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
17、【转载】Spring Boot整合Thymeleaf
自由之上
原创
1253人浏览过

Spring Boot 推荐使用 Thymeleaf 作为其模板引擎。
SpringBoot 为 Thymeleaf 提供了一系列默认配置,项目中一但导入了 Thymeleaf 的依赖,相对应的自动配置 (ThymeleafAutoConfiguration) 就会自动生效,因此 Thymeleaf 可以与 Spring Boot 完美整合 。

Spring Boot 整合 Thymeleaf 模板引擎,需要以下步骤:

  1. 引入 Starter 依赖
  2. 创建模板文件,并放在在指定目录下

1、引入依赖

Spring Boot 整合 Thymeleaf 的第一步,就是在项目的 pom.xml 中添加 Thymeleaf 的 Starter 依赖,代码如下。

  1. <!--Thymeleaf 启动器-->
  2. <dependency>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-starter-thymeleaf</artifactId>
  5. </dependency>

2、创建模板文件

Spring Boot 通过 ThymeleafAutoConfiguration 自动配置类对 Thymeleaf 提供了一整套的自动化配置方案,该自动配置类的部分源码如下。

  1. @Configuration(
  2. proxyBeanMethods = false
  3. )
  4. @EnableConfigurationProperties({ThymeleafProperties.class})
  5. @ConditionalOnClass({TemplateMode.class, SpringTemplateEngine.class})
  6. @AutoConfigureAfter({WebMvcAutoConfiguration.class, WebFluxAutoConfiguration.class})
  7. public class ThymeleafAutoConfiguration {
  8. }

ThymeleafAutoConfiguration 使用 @EnableConfigurationProperties 注解导入了 ThymeleafProperties 类,该类包含了与 Thymeleaf 相关的自动配置属性,其部分源码如下。

  1. @ConfigurationProperties(
  2. prefix = "spring.thymeleaf"
  3. )
  4. public class ThymeleafProperties {
  5. private static final Charset DEFAULT_ENCODING;
  6. public static final String DEFAULT_PREFIX = "classpath:/templates/";
  7. public static final String DEFAULT_SUFFIX = ".html";
  8. private boolean checkTemplate = true;
  9. private boolean checkTemplateLocation = true;
  10. private String prefix = "classpath:/templates/";
  11. private String suffix = ".html";
  12. private String mode = "HTML";
  13. private Charset encoding;
  14. private boolean cache;
  15. ...
  16. }

ThymeleafProperties 通过 @ConfigurationProperties 注解将配置文件(application.properties/yml) 中前缀为 spring.thymeleaf 的配置和这个类中的属性绑定。

在 ThymeleafProperties 中还提供了以下静态变量:

  • DEFAULT_ENCODING:默认编码格式
  • DEFAULT_PREFIX:视图解析器的前缀
  • DEFAULT_SUFFIX:视图解析器的后缀

根据以上配置属性可知,Thymeleaf 模板的默认位置在 resources/templates 目录下,默认的后缀是 html,即只要将 HTML 页面放在“classpath:/templates/”下,Thymeleaf 就能自动进行渲染。
与 Spring Boot 其他自定义配置一样,我们可以在 application.properties/yml 中修改以 spring.thymeleaf 开始的属性,以实现修改 Spring Boot 对 Thymeleaf 的自动配置的目的。

示例

  1. 创建一个名为 hello.html 的页面,并将该页面放在项目类路径(resources)下的 templates 目录中,hello.html 代码如下。
  1. <!DOCTYPE html>
  2. <!--导入thymeleaf的名称空间-->
  3. <html lang="en" xmlns:th="http://www.thymeleaf.org">
  4. <head>
  5. <meta charset="UTF-8">
  6. <title>Title</title>
  7. </head>
  8. <body>
  9. <!--th:text 为 Thymeleaf 属性,用于获取指定属性的值-->
  10. <h1 th:text="'欢迎来到'+${name}"></h1>
  11. </body>
  12. </html>
  1. 新建一个控制类 HelloController,并通过参数 map 传递数据到前台页面中,代码如下。
  1. package net.biancheng.www.controller;
  2. import org.springframework.stereotype.Controller;
  3. import org.springframework.web.bind.annotation.RequestMapping;
  4. import java.util.Map;
  5. @Controller
  6. public class HelloController {
  7. @RequestMapping("/hello")
  8. public String hello(Map<String, Object> map) {
  9. //通过 map 向前台页面传递数据
  10. map.put("name", "编程帮(www.biancheng.net)");
  11. return "hello";
  12. }
  13. }
  1. 启动 Spring Boot,使用浏览器访问“http://localhost:8080/hello”,结果如下图。

图1:Thymleaf 整合

本博文版权归博主所有,转载请注明地址!如有侵权、违法,请联系admin@php.cn举报处理!
全部评论 文明上网理性发言,请遵守新闻评论服务协议
0条评论
作者最新博文
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号

  • 登录PHP中文网,和优秀的人一起学习!
    全站2000+教程免费学