Fun SpringBoot 2 rapid integration | Thymeleaf articles

Foreword

Thymeleaf is a stand-alone environment for Web and modern server-side Java template engine.

The main goal is to bring Thymeleaf elegant natural template for your development workflow - HTML that can be displayed correctly in the browser, it can also be used as a static prototype, in order to achieve a more powerful collaboration across the development team.

By Spring Framework module, integrated with a large number of your favorite tools, and the ability to insert your own functions, Thymeleaf is the ideal choice for modern HTML5 JVM Web development - although it could have done more.
- excerpt from Thymeleaf official website link: https: //www.thymeleaf.org/index.html

How does this compare with Velocity or FreeMarker and other Thymeleaf other template engine?

Velocity and FreeMarker are all great software, but the principle issues they deal with Thymeleaf template is completely different.

Thymeleaf great emphasis on natural templated - Allows templates become a working prototype, while the other two templates are not allowed to do so - it's syntax attempt (to say) cleaner, more in line with current trends in web development. Further, from an architectural standpoint, Velocity and FreeMarker text processor are sequential, and is based on Thymeleaf labeled analytic techniques. This allows the use of specific Thymeleaf interesting characteristic mark on the environment, especially the web.

In any case, the best way to compare these techniques is to use them yourself, and feel what is best for you.

Above from Thymeleaf official website link: https://www.thymeleaf.org/faq.html#compare-other-engines

SpringBoot procedure using Thymeleaf

The first step is to introduce Thymeleaf starter dependent, specific code as follows:

        <!-- thymeleaf 相关依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

The second step is to add application.properties Thymeleaf configuration, the specific configuration is as follows:

server.port=8090
server.servlet.context-path=/sbe

#关闭 Thymeleaf 的缓存开发过程中无需重启
spring.thymeleaf.cache = false
#设置thymeleaf页面的编码
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.mode=HTML5
#设置thymeleaf页面的后缀
spring.thymeleaf.suffix=.html
#设置thymeleaf页面的存储路径
spring.thymeleaf.prefix=classpath:/templates/

The third step is to write access Thymeleaf page Controller.

@Controller
@RequestMapping("/hello")
public class ThymeleafHelloWrodController {
    
    @RequestMapping("/thymeleaf")
    public String helloThymeleaf(Model model){
        model.addAttribute("hello","hello Thymeleaf!");
        return "hello/index";
    }
}

Thymeleaf page code is as follows:

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
    <p th:text="${hello}">hello</p>
</body>
</html>

test

Enter Access Controller Thymeleaf page of the tour is at URL: http: // localhost: 8090 / sbe / hello / thymeleaf test, the test results are as follows:
Here Insert Picture Description

summary

SpringBoot Thymeleaf using the following steps:

  1. Introducing Thymeleaf starter dependent
  2. Add Thymeleaf configuration
  3. Write access Thymeleaf page and Controller

The sample code

Specific code examples, please see the next helloword package under the spring-boot-2.x-thymeleaf my GitHub repository springbootexamples of view.

GitHub:https://github.com/zhuoqianmingyue/springbootexamples

If you are interested in these, welcomed the star, or point-like support! Please indicate the source forwarding!

Guess you like

Origin www.cnblogs.com/jerry126/p/11531311.html