Spring Boot 2.0 Integrated thymeleaf template engine

Source: Internet
Author: User

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> &LT;ARTIFACTID&GT;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> &lt ;groupid>org.springframework.boot</groupid> &LT;ARTIFACTID&GT;SPRING-BOOT-MAVEN-PLUGIN&LT;/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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.