Thymeleaf

Thymeleaf 变量

1、简介 本文将通过一个 Spring Boot 示例带你了解 Thymeleaf 中的变量。 2、Maven 依赖 要使用 Thymeleaf,需要添加 spring-boot-starter-thymeleaf 和 spring-boot-starter-web 依赖: <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> 3、Web Controller 首先,创建一个带有 GET 端点的 Web Controller,该端点返回一个包含文章列表的页面。 @GetMapping 方法只接受一个参数 - Model。它包含所有可在 Thymeleaf 模板中使用的全局变量。在本例中,Model 只有一个参数,即文章列表。 Article 类由两个 String 字段(name 和 url)组成: public class Article { private String name; private String url; // 构造函数、get、set 方法省略 } Controller 方法的返回值应该是要渲染的 Thymeleaf 模板的名称。该名称应与 src/resource/template 目录中的 HTML 文件相对应。在本例中,就是 src/resource/template/articles-list.html。 Controller 如下: @Controller @RequestMapping("/api/articles") public class ArticlesController { @GetMapping public String allArticles(Model model) { model.

Spring 整合 Thymeleaf 模板引擎

1、概览 Thymeleaf 是一个 Java 模板引擎,用于处理和创建 HTML、XML、JavaScript、CSS 和文本。 本文将带你了解如何在 Spring 和 Spring Boot 应用中整合、使用 Thymeleaf。 该库具有极高的可扩展性,其天然的模板功能可以确保在没有后端的情况下制作模板原型。与其他流行的模板引擎(如 JSP)相比,这使得开发速度非常快。 2、Spring 整合 Thymeleaf 首先,来看看与 Spring 整合所需的配置。这需要使用 thymeleaf-spring 库。 在 Maven POM 文件中添加以下依赖: <dependency> <groupId>org.thymeleaf</groupId> <artifactId>thymeleaf</artifactId> <version>3.1.2.RELEASE</version> </dependency> <dependency> <groupId>org.thymeleaf</groupId> <artifactId>thymeleaf-spring5</artifactId> <version>3.1.2.RELEASE</version> </dependency> 注意,对于 Spring 4,必须使用 thymeleaf-spring4,而不是 thymeleaf-spring5。 通过 SpringTemplateEngine 类执行所有配置步骤。 可以在 Java 配置中将该类配置为 bean: @Bean @Description("Thymeleaf Template Resolver") public ServletContextTemplateResolver templateResolver() { ServletContextTemplateResolver templateResolver = new ServletContextTemplateResolver(); templateResolver.setPrefix("/WEB-INF/views/"); templateResolver.setSuffix(".html"); templateResolver.setTemplateMode("HTML5"); return templateResolver; } @Bean @Description("Thymeleaf Template Engine") public SpringTemplateEngine templateEngine() { SpringTemplateEngine templateEngine = new SpringTemplateEngine(); templateEngine.

在 Spring 中使用 Thymeleaf 显示错误信息

1、概览 本文将带你了解如何在 Spring 应用中使用 Thymeleaf 模板来渲染错误信息。 我们会通过一个简单的 Spring Boot 项目来进行演示,该项目是一个 “用户注册” 应用,需要验证客户端传递的各个字段,还要处理全局错误。 2、Spring Boot 应用示例 创建一个简单的 Spring Boot 用户注册应用,需要一个 Controller、一个 Repository 和一个 Entity。 2.1、Maven 依赖 添加所有需要的 Spring Boot Starter:Web Mvc、Hibernate Validation、 Thymeleaf 和 JPA。 此外,还需要一个 H2 内存数据库依赖: <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-validation</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <scope>runtime</scope> <version>1.4.200</version> </dependency> 2.2、实体 User 实体如下: @Entity public class User { @Id @GeneratedValue(strategy = GenerationType.

Thymeleaf 中 th:text 和 th:value 之间的区别

1、概览 Thymeleaf 是一种流行的 Java 模板引擎,它允许我们创建动态网页。它提供了多种属性,用于将数据从模型绑定到视图。 在本教程中,我们将了解 Thymeleaf 中 th:text 和 th:value 属性之间的主要区别。 2、th:text 属性 Thymeleaf 中的 th:text 属性用于设置元素的文本内容。 它还取代了标准的 HTML text 属性。因此,我们可以把它放在任何支持文本内容的 HTML 元素中,如标题、段落、标签等。 我们还可以使用该属性来显示动态文本内容,例如网页上的标题。 假设我们想在 HTML 页面上显示 controller 提供的 title 属性。 首先,让我们创建一个 controller 类和一个指定模型属性的方法: @GetMapping public String show(Model model) { model.addAttribute("title", "Baeldung"); return "attributes/index"; } 接下来,我们将在标题元素中显示值: <h1 th:text="${title}"/> 在这里,Thymeleaf 会计算表达式 ${title},并将该值插入标题元素。 我们将得到的 HTML 如下: <h1>Baeldung</h1> 此外,与标准 HTML text 属性不同,th:text 属性支持表达式。除了变量,这些表达式还可能包括运算符和函数。 例如,让我们在没有提供 title 属性的情况下指定默认值: <h1 th:text="${title} ?: 'Default title'"/> 3、th:value 属性 另一方面,th:value 属性用于设置通常需要用户输入的元素的值。输入框、复选框、单选按钮和下拉按钮等元素都属于此类。