This section will work with you. Spring Boot 2.0 and thymeleaf template engine
1. Create a project
2. Quickly create a spring Boot application using spring INITLIZR
3. Fill in the project configuration information
4. Add a Web Module
5. Add Thymeleaf Module
6. Project Save path
7. Pom.xml Add Thymeleaf Dependency
<?xml version= "1.0" encoding= "UTF-8"? ><project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns:xsi= "http ://www.w3.org/2001/XMLSchema-instance "xsi:schemalocation= "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" > <modelversion>4.0.0 </modelVersion> <groupId>com.xingyun</groupId> <artifactId> Srping-boot-with-thymeleaf-sample</artifactid> <version>0.0.1-SNAPSHOT</version> <packaging >jar</packaging> <name>srping-boot-with-thymeleaf-sample</name> <description>demo Project for Spring boot</description> <parent> <groupId>org.springframework.boot</groupId> & Lt;artifactid>spring-boot-starter-parent</artifactid> <version>2.0.1.RELEASE</version> <relativePath/> <!--lookup parent from repository to </parent> <properties> <pro Ject.build.sourceencoding>utf-8</project.build.sourceencoding> <project.reporting.outputencoding >UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties& Gt <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <ARTIFACTID>SPRING-BOOT-STARTER-WEB&L T;/artifactid> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </de pendency> </dependencies> <build> <plugins> <plugin> < ;groupid>org.springframework.boot</groupid> <ARTIFACTID>SPRING-BOOT-MAVEN-PLUGIN</ARTIFAC tid> </plugin> </plugins> </build></project>
8. Configure Application.properties
spring.thymeleaf.prefix=classpath:/templates/Spring.thymeleaf.suffix=. Htmlspring.thymeleaf.mode =HTMLspring.thymeleaf.encoding=utf-8spring.thymeleaf.servlet.content-type=text/html # Close cache, instant refresh, on-line production environment must be changed to Truespring.thymeleaf.cache=false
9. Create a template file
Index.html
<!DOCTYPE HTML><HTMLLang= "en"><Head> <MetaCharSet= "UTF-8"> <title>Title</title></Head><Body>This is index.html</Body></HTML>
Welcome.html
<!DOCTYPE HTML><HTMLLang= "en"><Head> <MetaCharSet= "UTF-8"> <title>Title</title></Head><Body>This is welcome.html</Body></HTML>
10. Create a Controller
Packagecom.xingyun.srpingbootwiththymeleafsample;ImportOrg.springframework.stereotype.Controller;Importorg.springframework.web.bind.annotation.GetMapping;Importorg.springframework.web.bind.annotation.RequestMapping; @Controller Public classHomeController {@GetMapping (value= "/") PublicString Index () {return"/views/index"; } @GetMapping (Value= "/welcome") PublicString Welcome () {return"/views/welcome"; }}
11. Visit http://127.0.0.1:8080
12. Visit Http://127.0.0.1:8080/welcome
The source code of this blog post
Spring Boot 2.0 Integrated thymeleaf template engine