温馨提示×

springboot整合thymeleaf的方法是什么

小亿
90
2024-02-23 18:01:34
栏目: 编程语言

  1. 首先,在pom.xml文件中添加Thymeleaf的依赖:
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
  1. 确保在application.properties文件中配置Thymeleaf的模板路径:
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
  1. 创建一个Controller类,用于处理页面请求,并返回Thymeleaf模板:
@Controller
public class MyController {

    @RequestMapping("/myPage")
    public String myPage(Model model) {
        model.addAttribute("message", "Hello, Thymeleaf!");
        return "myPage";
    }
}
  1. 创建一个Thymeleaf模板文件(myPage.html),并在文件中使用Thymeleaf的语法进行页面渲染:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>My Page</title>
</head>
<body>
    <h1 th:text="${message}"></h1>
</body>
</html>
  1. 启动Spring Boot应用程序,并访问http://localhost:8080/myPage,即可看到页面上显示"Hello, Thymeleaf!"。

通过以上步骤,就可以实现Spring Boot整合Thymeleaf的功能。

0