学堂 学堂 学堂公众号手机端

首先,在pom.xml文件中添加Thymeleaf的依赖: <dependency> <groupId>org.springframework.boot

lewis 1年前 (2024-02-04) 阅读数 4 #技术

  1. 首先,在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模板:
  • @Controller publicclassMyController{ @RequestMapping("/myPage") publicStringmyPage(Modelmodel){ model.addAttribute("message","Hello,Thymeleaf!"); return"myPage"; } }
  • 创建一个Thymeleaf模板文件(myPage.html),并在文件中使用Thymeleaf的语法进行页面渲染:
  • <!DOCTYPEhtml> <htmlxmlns:th="http://www.thymeleaf.org"> <head> <title>MyPage</title> </head> <body> <h1th:text="${message}"></h1> </body> </html>
  • 启动SpringBoot应用程序,并访问http://localhost:8080/myPage,即可看到页面上显示"Hello,Thymeleaf!"。
  • 通过以上步骤,就可以实现SpringBoot整合Thymeleaf的功能。


    版权声明

    本文仅代表作者观点,不代表博信信息网立场。

    热门