本文为SpringBoot工程配置Thymeleaf前端模板
在pom.xml文件中添加Thymeleaf依赖
…
//集成Thymeleaf
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
…
修改application.yml文件
server:
port: 9000
# thymeleaf
spring:
thymeleaf:
encoding: UTF-8
prefix: classpath:/templates/
check-template-location: true
suffix: .html
servlet:
content-type: text/html
mode: HTML5
cache: false
修改HelloController.java文件中的hello方法
@RequestMapping(value = "/hello",method = RequestMethod.GET)
public String hello(Model model){
model.addAttribute("content","hello world");
return "index";
}
在templates文件夹下创建index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<p th:text="${content}"></p>
</body>
</html>
启动工程,在浏览器输入http://localhost:9000/hello,结果如下
完成Thymeleaf添加
这篇博客详细介绍了如何在SpringBoot项目中配置Thymeleaf作为前端模板引擎,包括在pom.xml中添加Thymeleaf依赖,更新application.yml配置,修改HelloController以及创建并设置index.html页面。通过这些步骤,最终能够在本地启动工程并在浏览器中预览效果。
253

被折叠的 条评论
为什么被折叠?



