目录
二、配置Thymeleaf2.1模板位置配置可以通过配置 spring.thymeleaf.prefix 和 spring.thymeleaf.suffix 来指定模板文件的位置和后缀
3.3在HTML文件中使用Thymeleaf语法来定义模板内容
4.1在Spring Boot中,在pom.xml文件中添加以下依赖项
4.2在Spring MVC控制器类中,使用@Controller注解标记该类,并使用@RequestMapping注解定义处理请求的方法。
一、添加依赖
1.1首先,在项目的构建文件中(比如 Maven 或 Gradle)添加 Thymeleaf 的依赖。例如,对于 Maven 项目,在 pom.xml 文件中添加以下依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
1.2保存并更新项目依赖
这样就完成了Thymeleaf的引入。在你的代码中,你可以使用Thymeleaf提供的标签和表达式来处理模板中的动态内容,并将其渲染为最终的HTML页面。
二、配置Thymeleaf
2.1模板位置配置
可以通过配置 spring.thymeleaf.prefix 和 spring.thymeleaf.suffix 来指定模板文件的位置和后缀
spring.thymeleaf.prefix=/WEB-INF/templates/
spring.thymeleaf.suffix=.html
上述配置将会使 Thymeleaf 在 /WEB-INF/templates/ 目录下查找以 .html 结尾的模板文件。
2.2模板缓存配置
Thymeleaf 默认开启了模板缓存,以提高性能。在开发阶段可能需要关闭缓存以方便调试,可以通过配置 spring.thymeleaf.cache 进行设置。
spring.thymeleaf.cache=false
上述配置将会使Thymeleaf的模板缓存
2.3自定义标签配置
Thymeleaf 支持自定义标签,可以在配置中注册自定义标签处理器。
@Configuration
public class ThymeleafConfig implements ITemplateResolver {
// ...其他配置
@Bean
public SpringResourceTemplateResolver templateResolver() {
SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver();
// 配置模板位置、缓存等
// ...
return templateResolver;
}
@Bean
public SpringTemplateEngine templateEngine() {
SpringTemplateEngine templateEngine = new SpringTemplateEngine();
templateEngine.setTemplateResolver(templateResolver());
// 注册自定义标签处理器
// ...
return templateEngine;
}
@Bean
public ThymeleafViewResolver viewResolver() {
ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
viewResolver.setTemplateEngine(templateEngine());
// 其他配置
// ...
return viewResolver;
}
// ...其他配置
}
上述代码中,通过 templateResolver() 方法配置模板解析器,通过 templateEngine() 方法配置模板引擎,最后通过 viewResolver() 方法配置视图解析器。在 templateResolver() 和 templateEngine() 方法中可以设置一些自定义的属性,如模板位置、缓存等。在 templateEngine() 方法中还可以注册自定义的标签处理器。
三、创建模板文件
3.1创建一个HTML文件
将其命名为模板名称,如“template.html"。
3.2在HTML文件中引入Thymeleaf命名空间
<h1 th:text="${title}">Page Title</h1>
<p th:text="${content}">Page Content</p>
3.3在HTML文件中使用Thymeleaf语法来定义模板内容
<h1 th:text="${title}">Page Title</h1>
<p th:text="${content}">Page Content</p>
3.4在Java代码中加载模板,并将数据传递给模板
// 加载模板文件
Template template = thymeleafTemplateEngine.getTemplate("template.html");
// 创建一个上下文对象,用于传递数据给模板
Context context = new Context();
context.setVariable("title", "My Page Title");
context.setVariable("content", "Hello, world!");
// 渲染模板并生成HTML代码
String renderedHtml = templateEngine.process(template, context);
在上面的例子中,我们使用Thymeleaf模板引擎加载模板文件"template.html",然后创建一个上下文对象并通过它将数据传递给模板。最后,我们调用process()
方法来渲染模板,并生成HTML代码
四、控制器中使用Thymeleaf
4.1在Spring Boot中,在pom.xml文件中添加以下依赖项
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
4.2在Spring MVC控制器类中,使用@Controller
注解标记该类,并使用@RequestMapping
注解定义处理请求的方法。
@Controller
public class MyController {
@RequestMapping("/hello")
public String hello(Model model) {
model.addAttribute("message", "Hello, Thymeleaf!");
return "hello";
}
}