在application.properties中配置Thymeleaf
时间: 2023-07-24 19:39:18 浏览: 39
可以按照以下步骤在application.properties中配置Thymeleaf:
1. 在application.properties中添加以下配置:
```
spring.thymeleaf.enabled=true
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
```
2. 解释一下这些配置的含义:
- `spring.thymeleaf.enabled`:是否启用Thymeleaf,默认为true。
- `spring.thymeleaf.prefix`:Thymeleaf模板文件的位置,默认为`classpath:/templates/`,即在项目的resources目录下的templates文件夹中。
- `spring.thymeleaf.suffix`:Thymeleaf模板文件的后缀名,默认为`.html`。
3. 在项目中使用Thymeleaf模板时,可以直接使用`@Controller`注解的控制器方法返回对应的模板文件名,Thymeleaf会自动在配置的模板文件位置下寻找对应的模板文件,例如:
```
@Controller
public class MyController {
@RequestMapping("/")
public String index(Model model) {
model.addAttribute("name", "World");
return "index";
}
}
```
这里的`return "index"`表示返回名为`index`的Thymeleaf模板文件,模板文件应该放置在`classpath:/templates/`下,并且文件名应该为`index.html`。