在Spring Boot项目中,添加Thymeleaf依赖非常简单。只需要在`pom.xml`文件中添加以下依赖即可:
```xml
org.springframework.boot
spring-boot-starter-thymeleaf
```
这个依赖会自动引入Thymeleaf模板引擎和相关的依赖库。添加完依赖后,你可以在控制器方法中返回Thymeleaf模板名称,例如:
```java
@GetMapping("/hello")
public String hello(Model model) {
model.addAttribute("name", "Thymeleaf");
return "hello";
}
```
在上面的例子中,控制器方法返回了一个名为`hello`的字符串,它被解释为Thymeleaf模板的名称。模板文件应该放置在`src/main/resources/templates`目录下,并以`.html`作为扩展名。
在模板文件中,你可以使用Thymeleaf的语法来渲染页面内容,例如:
```html
Hello Thymeleaf
Hello, !
```
在上面的例子中,使用了Thymeleaf的表达式`${name}`来显示控制器方法中传递的`name`参数。当页面被渲染时,Thymeleaf会将`${name}`替换为实际的参数值。