原文: http://zetcode.com/articles/springbootthymeleafconf/

在 Spring Boot Thymeleaf 配置教程中,我们将展示如何使用 Spring Boot Web 应用配置 Thymeleaf。 当 Spring Boot 在 Maven POM 文件中找到 Thymeleaf 依赖项时,它会自动配置 Thymeleaf 模板引擎。 本教程显示了如何在 Java 配置中手动进行操作。

Spring 是流行的 Java 应用框架。 Spring Boot 致力于以最小的努力创建独立的,基于生产级别的基于 Spring 的应用。

Thymeleaf

Thymeleaf 是适用于 Web 和独立环境的现代服务器端 Java 模板引擎。 它基于自然模板的概念:模板文件可以在浏览器中直接打开,并且仍然可以正确显示为网页。

Spring Boot Thymeleaf 示例

以下示例使用 Java 配置通过 Spring Boot 设置 Thymeleaf。

  1. pom.xml
  2. src
  3. ├───main
  4. ├───java
  5. └───com
  6. └───zetcode
  7. Application.java
  8. └───config
  9. WebConfig.java
  10. └───resources
  11. └───mytemplates
  12. index.html
  13. └───test
  14. └───java

这是项目结构。 Thymeleaf 模板文件位于自定义src/main/resources/mytemplates目录中。 默认模板目录为src/main/resources/templates

pom.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
  5. http://maven.apache.org/xsd/maven-4.0.0.xsd">
  6. <modelVersion>4.0.0</modelVersion>
  7. <groupId>com.zetcode</groupId>
  8. <artifactId>thymeleafconfigex</artifactId>
  9. <version>1.0-SNAPSHOT</version>
  10. <packaging>jar</packaging>
  11. <properties>
  12. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  13. <maven.compiler.source>11</maven.compiler.source>
  14. <maven.compiler.target>11</maven.compiler.target>
  15. </properties>
  16. <parent>
  17. <groupId>org.springframework.boot</groupId>
  18. <artifactId>spring-boot-starter-parent</artifactId>
  19. <version>2.1.0.RELEASE</version>
  20. </parent>
  21. <dependencies>
  22. <dependency>
  23. <groupId>org.springframework.boot</groupId>
  24. <artifactId>spring-boot-devtools</artifactId>
  25. <optional>true</optional>
  26. </dependency>
  27. <dependency>
  28. <groupId>org.springframework.boot</groupId>
  29. <artifactId>spring-boot-starter-web</artifactId>
  30. <optional>true</optional>
  31. </dependency>
  32. <dependency>
  33. <groupId>org.springframework.boot</groupId>
  34. <artifactId>spring-boot-starter-thymeleaf</artifactId>
  35. </dependency>
  36. </dependencies>
  37. <build>
  38. <plugins>
  39. <plugin>
  40. <groupId>org.springframework.boot</groupId>
  41. <artifactId>spring-boot-maven-plugin</artifactId>
  42. </plugin>
  43. </plugins>
  44. </build>
  45. </project>

这是 Maven 构建文件。 spring-boot-devtools启用热插拔,禁用模板缓存并启用实时重新加载。 spring-boot-starter-thymeleaf是使用 Thymeleaf 构建 Spring MVC 应用的入门工具。 spring-boot-starter-web是 Web 应用的启动器。

com/zetcode/config/WebConfig.java

  1. package com.zetcode.config;
  2. import org.springframework.context.annotation.Bean;
  3. import org.springframework.context.annotation.Configuration;
  4. import org.springframework.context.annotation.Description;
  5. import org.springframework.web.servlet.ViewResolver;
  6. import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
  7. import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
  8. import org.thymeleaf.spring5.SpringTemplateEngine;
  9. import org.thymeleaf.spring5.view.ThymeleafViewResolver;
  10. import org.thymeleaf.templateresolver.ClassLoaderTemplateResolver;
  11. @Configuration
  12. public class WebConfig implements WebMvcConfigurer {
  13. @Bean
  14. @Description("Thymeleaf template resolver serving HTML 5")
  15. public ClassLoaderTemplateResolver templateResolver() {
  16. var templateResolver = new ClassLoaderTemplateResolver();
  17. templateResolver.setPrefix("mytemplates/");
  18. templateResolver.setCacheable(false);
  19. templateResolver.setSuffix(".html");
  20. templateResolver.setTemplateMode("HTML5");
  21. templateResolver.setCharacterEncoding("UTF-8");
  22. return templateResolver;
  23. }
  24. @Bean
  25. @Description("Thymeleaf template engine with Spring integration")
  26. public SpringTemplateEngine templateEngine() {
  27. var templateEngine = new SpringTemplateEngine();
  28. templateEngine.setTemplateResolver(templateResolver());
  29. return templateEngine;
  30. }
  31. @Bean
  32. @Description("Thymeleaf view resolver")
  33. public ViewResolver viewResolver() {
  34. var viewResolver = new ThymeleafViewResolver();
  35. viewResolver.setTemplateEngine(templateEngine());
  36. viewResolver.setCharacterEncoding("UTF-8");
  37. return viewResolver;
  38. }
  39. @Override
  40. public void addViewControllers(ViewControllerRegistry registry) {
  41. registry.addViewController("/").setViewName("index");
  42. }
  43. }

WebConfig中,我们配置 Thymeleaf 并设置主页的视图和控制器。 模板引擎是用 Java 代码配置的。

  1. @Bean
  2. @Description("Thymeleaf template resolver serving HTML 5")
  3. public ClassLoaderTemplateResolver templateResolver() {

这个 bean 定义了一个模板解析器。 模板解析器将模板解析为TemplateResolution对象,其中包含其他信息,例如模板模式,缓存,模板的前缀和后缀。 ClassLoaderTemplateResolver用于加载位于类路径上的模板。

  1. templateResolver.setPrefix("mytemplates/");

我们将模板目录设置为mytemplates。 使用ClassLoaderTemplateResolver时,前缀中没有classpath:

  1. templateResolver.setTemplateMode("HTML5");

模板引擎将提供 HTML5 内容。

  1. @Bean
  2. @Description("Thymeleaf template engine with Spring integration")
  3. public SpringTemplateEngine templateEngine() {
  4. var templateEngine = new SpringTemplateEngine();
  5. templateEngine.setTemplateResolver(templateResolver());
  6. return templateEngine;
  7. }

创建具有 Spring 集成的 Thymeleaf 模板引擎。

  1. @Bean
  2. @Description("Thymeleaf view resolver")
  3. public ViewResolver viewResolver() {
  4. var viewResolver = new ThymeleafViewResolver();
  5. viewResolver.setTemplateEngine(templateEngine());
  6. viewResolver.setCharacterEncoding("UTF-8");
  7. return viewResolver;
  8. }

在这里,我们配置一个创建ThymeleafViewResolver的 bean。 视图解析器负责获取特定操作和语言环境的 View 对象。 然后将视图对象渲染为 HTML 文件。

  1. @Override
  2. public void addViewControllers(ViewControllerRegistry registry) {
  3. registry.addViewController("/").setViewName("index");
  4. }

在这个简单的应用中,我们没有特定的控制器类。 我们用addViewController()方法定义一个自动控制器。

resources/templates/index.html

  1. <!DOCTYPE html>
  2. <html xmlns:th="http://www.thymeleaf.org">
  3. <head>
  4. <title>Home page</title>
  5. <meta charset="UTF-8"/>
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
  7. </head>
  8. <body>
  9. <p>
  10. <span th:text="'Today is: ' + ${#dates.format(#dates.createNow(), 'dd MMM yyyy HH:mm')}" th:remove="tag"></span>
  11. </p>
  12. </body>
  13. </html>

这是 Thymeleaf 模板文件。 它显示当前日期。

com/zetcode/Application.java

  1. package com.zetcode;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. @SpringBootApplication
  5. public class Application {
  6. public static void main(String[] args) {
  7. SpringApplication.run(Application.class, args);
  8. }
  9. }

这段代码设置了 Spring Boot 应用。 @SpringBootApplication启用自动配置和组件扫描。

  1. $ mvn spring-boot:run

我们启动该应用。

  1. $ curl localhost:8080
  2. <!DOCTYPE html>
  3. <html>
  4. <head>
  5. <title>Home page</title>
  6. <meta charset="UTF-8">
  7. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  8. </head>
  9. <body>
  10. <p>
  11. Today is: 17 Jan 2019 23:46
  12. </p>
  13. </body>

在本教程中,我们使用 Thymeleaf 创建了 Spring Boot Web 应用。 您可能也对相关教程感兴趣: Spring Boot Thymeleaf 教程Spring Boot 自动控制器Spring Boot FreeMarker 教程Spring Boot Swing 集成教程Spring Web 应用简介独立的 Spring 应用FreeMarker 教程Java 教程