当前位置: 首页 > 建站教程

springboot整合thymeleaf的方法是什么

时间:2026-01-28 13:46:46

    首先,在pom.xml文件中添加Thymeleaf的依赖:
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId></dependency>
    确保在application.properties文件中配置Thymeleaf的模板路径:
spring.thymeleaf.prefix=classpath:/templates/spring.thymeleaf.suffix=.html
    创建一个Controller类,用于处理页面请求,并返回Thymeleaf模板:
@Controllerpublic class MyController {@RequestMapping("/myPage")public String myPage(Model model) {model.addAttribute("message", "Hello, Thymeleaf!");return "myPage";}}
    创建一个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>
    启动Spring Boot应用程序,并访问http://localhost:8080/myPage,即可看到页面上显示"Hello, Thymeleaf!"。

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


上一篇:Java中localdatetime怎么获取毫秒值
下一篇:Java中scheduledexecutorservice是什么
springboot thymeleaf
  • 英特尔与 Vertiv 合作开发液冷 AI 处理器
  • 英特尔第五代 Xeon CPU 来了:详细信息和行业反应
  • 由于云计算放缓引发扩张担忧,甲骨文股价暴跌
  • Web开发状况报告详细介绍可组合架构的优点
  • 如何使用 PowerShell 的 Get-Date Cmdlet 创建时间戳
  • 美光在数据中心需求增长后给出了强有力的预测
  • 2027服务器市场价值将接近1960亿美元
  • 生成式人工智能的下一步是什么?
  • 分享在外部存储上安装Ubuntu的5种方法技巧
  • 全球数据中心发展的关键考虑因素
  • 英特尔与 Vertiv 合作开发液冷 AI 处理器

    英特尔第五代 Xeon CPU 来了:详细信息和行业反应

    由于云计算放缓引发扩张担忧,甲骨文股价暴跌

    Web开发状况报告详细介绍可组合架构的优点

    如何使用 PowerShell 的 Get-Date Cmdlet 创建时间戳

    美光在数据中心需求增长后给出了强有力的预测

    2027服务器市场价值将接近1960亿美元

    生成式人工智能的下一步是什么?

    分享在外部存储上安装Ubuntu的5种方法技巧

    全球数据中心发展的关键考虑因素