Springboot series: Springboot and Thymeleaf template engine integration basic tutorial (with source code)

foreword

Due to the use of a lot of technical integration in the development of the My Blog project, the use and integration process of some frameworks have not been introduced and recorded in detail, which has caused some friends to use it a little hard, so I plan to do some in the next time. The introduction of basic integration, of course, may not be particularly basic, but the source code will be open to everyone, so that everyone can learn, the source code address this time is springboot-thymeleaf , thank you for your support.

Introduction

Thymeleaf
Thymeleaf is a template engine similar to Velocity and FreeMarker. It can completely replace JSP. Compared with other template engines, it has the following three very attractive features:

  • Thymeleaf can run in both network and non-network environments, that is, it allows artists to view the static effect of the page in the browser, and also allows the programmer to view the dynamic page effect with data in the server. This is because it supports html prototypes, and then adds additional attributes in html tags to achieve template + data display. Browsers ignore undefined tag attributes when interpreting html, so thymeleaf templates can run statically; when data is returned to the page, the Thymeleaf tag will dynamically replace the static content, making the page dynamically display.
  • Thymeleaf features out of the box. It provides two dialects, standard and spring standard, and can directly apply templates to achieve JSTL and OGNL expression effects, avoiding the trouble of setting templates, changing jstl, and changing labels every day. Developers can also extend and create custom dialects.
  • Thymeleaf provides spring standard dialects and an optional module that is perfectly integrated with SpringMVC, which can quickly implement form binding, property editor, internationalization and other functions.

integration process

Edit the pom file and import Thymeleaf

<?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>
    <artifactId>com.my.blog</artifactId>
    <name>springboot-thymeleaf</name>
    <description>springboot-thymeleaf</description>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.1.RELEASE</version>
    </parent>

    <properties>
        <java.version>1.7</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

Thymeleaf placement

# thymeleaf
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.check-template-location=true
spring.thymeleaf.suffix=.html
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.content-type=text/html
spring.thymeleaf.mode=HTML5
spring.thymeleaf.cache=false

New template file

Add a templates directory under the resources folder to store template files, and add hello.html.

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8"/>
    <title>springboot-thymeleaf demo</title>
</head>
<body>
    <p th:text="'hello, ' + ${name} + '!'" />
</body>
</html>

Edit business code and start class

HelloController:

/**
 * author:13
 * date:2017-09-14
 */
@Controller
public class HelloController {
    @RequestMapping("/hello")
    public String hello(HttpServletRequest request, @RequestParam(value = "name", required = false, defaultValue = "springboot-thymeleaf") String name) {
        request.setAttribute("name", name);
        return "hello";
    }
}

WebApplication :

/**
 * author:13
 * date:2017-09-14
 */
@SpringBootApplication
public class WebApplication extends SpringBootServletInitializer {
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(WebApplication.class);
    }

    public static void main(String[] args) throws Exception {
        SpringApplication.run(WebApplication.class, args);
    }
}

verify

After the project starts, enter the following url in the browser http://localhost:8080/hello:
hello-thymeleaf

Epilogue

First published on my personal blog .

If you have any questions or have some good ideas, please leave me a message, and thank you for pointing out the problems in the project.

The code and this problem are all in the My Blog project. If you want to continue to understand the project, you can check the entire series of articles Java open source blog My-Blog (SpringBoot+Docker) series articles , you can also go to my GitHub repository or open source China View the source code and detailed deployment process and usage documentation in the code repository .

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325762633&siteId=291194637