Thymeleaf是一个java类库,他是一个xml/xhtml/html5的模板引擎和Struts框架的freemarker模板类似,可以作为mvc的web应用的view层。Thymeleaf还提供了额外的模块与Spring MVC集成,所以我们可以使用Thymeleaf完全替代jsp。spring Boot通过org.springframework.boot.autoconfigure.thymeleaf包对Thymeleaf进行了自动配置。通过ThymeleafAutoConfiguration类对集成所需要的bean进行自动配置。包括templateResolver,templateEngine,thymeleafViewResolver的配置。
Add in 1.pom.xml
<dependency><groupId>org.springframework.boot</groupId><artifactId> Spring-boot-starter-thymeleaf</artifactId></dependency>
Download the Thymeleaf jar package automatically, and then update Project.
Add Thymeleaf profile in 2.application.properties
#Thymeleafspring. Thymeleaf.prefix=classpath:/templates/spring.thymeleaf.suffix=. Htmlspring.thymeleaf.mode=HTML5spring.thymeleaf.encoding=utf-8#; CharSet=<encoding > is added#spring.thymeleaf.content-type=text/false for Hot Refreshspring.thymeleaf.cache=false
3. Then write a test
A. In the project directory, look for Src/main/resources/templates to create a new dome.html Test Web page, Note the header reference
<!DOCTYPE HTML> <HTMLxmlns= "http://www.w3.org/1999/xhtml"xmlns:th= "http://www.thymeleaf.org"xmlns:sec= "Http://www.thymeleaf.org/thymeleaf-extras-springsecurity3"> <Head> <titleTh:text= "${title}"></title> </Head> <Body> <PTh:text= "${gr}"></P> </Body> </HTML>
B. Write test controller Layer Thymeleaf.class class
PackageCom.hxzy.myblog.controller;ImportOrg.springframework.stereotype.Controller;ImportOrg.springframework.ui.Model;Importorg.springframework.web.bind.annotation.RequestMapping; @Controller Public classThymeleaf {@RequestMapping ("/hi") PublicString hellohtml (model model) {Model.addattribute ("title", "Thymeleaf"); Model.addattribute ("Gr", "Good everyone"); return"Dome"; }}
4. Summary:
1. Note that tags in HTML must be tightly closed
2.html in a pair of tags using thymeleaf, with th:text= "${title}", the title is added to the tag, such as
<title th:text= "${title}" ></title>.
Reference and application of the Thymeleaf template in spring boot