在maven項目的pom.xml中引入spingboot-boot-starter-thymeleaf依賴,同時爲了解決Html嚴格校驗報錯的問題,增長nekohtml依賴css
<!--thymeleaf模板引擎--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>net.sourceforge.nekohtml</groupId> <artifactId>nekohtml</artifactId> <version>1.9.15</version> </dependency>
server.port=8080
##取出thymeleaf的html的嚴格校驗
spring.thymeleaf.mode=LEGACYHTML5
##設定thymeleaf文件路徑,默認爲sc/main/resources/templates
spring.thymeleaf.prefix=classpath:/templates/
##設定靜態文件路徑,js、css等
spring.mvc.static-path-pattern=/static/**
##是否開啓模板緩存,默認true
##建議在開發時關閉模板緩存,否則沒法看到實時的頁面
spring.thymeleaf.cache=false
##模板編碼
spring.thymeleaf.encoding=UTF-8
(1)Controller控制層html
@Controller public class TestController { @RequestMapping("/") public String testThymeleaf(ModelMap modelMap){ modelMap.addAttribute("msg", "Hello , this is thymeleaf"); return "thymeleaf"; }
(2)Html頁面spring
<!DOCTYPE html> <!--解決th報錯 --> <html lang="en" xmlns:th="http://www.w3.org/1999/xhtml"> <head> <meta charset="UTF-8"> <title>thymeleaf</title> </head> <body> <h1 th:text="${msg}"></h1> </body> </html>
訪問:http://localhost:8080緩存
返回頂部mvc