Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use Thymeleaf operation in SpringBoot

Shulou Source: shulou.com Published: 2022-06-01 08:22:15 09月22日 Update

Most people do not understand the knowledge points of this article "how to use Thymeleaf in SpringBoot", so the editor summarizes the following contents, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "how to use Thymeleaf in SpringBoot" article.

Preface

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

The main goal of Thymeleaf is to bring elegant natural templates to your development workflow-HTML that can be displayed correctly in browsers or can also be used as static prototypes to achieve stronger collaboration in the development team.

With Spring Framework modules, extensive integration with your favorite tools, and the ability to plug in your own features, Thymeleaf is an ideal choice for modern HTML5 JVM Web development.

How does Thymeleaf compare to other template engines such as Velocity or FreeMarker?

Velocity and FreeMarker are both great software, but they deal with template problems in a completely different way than Thymeleaf.

Thymeleaf places a strong emphasis on natural templating-- allowing templates to become working prototypes, while the other two templates don't-- and its syntax tries (arguably) to be cleaner and more in line with current web development trends. In addition, from an architectural point of view, Velocity and FreeMarker are sequential text processors, while Thymeleaf is based on tag parsing technology. This allows Thymeleaf to take advantage of interesting features specific to tag-based environments, especially web.

Steps for SpringBoot to use Thymeleaf

The first step is to introduce Thymeleaf starter dependency. The specific code is as follows:

Org.springframework.boot spring-boot-starter-thymeleaf

The second step is to add Thymeleaf-related configuration in application.properties, as shown below:

Server.port=8090server.servlet.context-path=/sbe# shuts down Thymeleaf cache development process does not need to restart spring.thymeleaf.cache = false# setting thymeleaf page encoding spring.thymeleaf.encoding=UTF-8spring.thymeleaf.mode=HTML5# setting thymeleaf page suffix spring.thymeleaf.suffix=.html# setting thymeleaf page storage path spring.thymeleaf.prefix=classpath:/templates/

The third step is to write a Controller to visit the Thymeleaf page.

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

The Thymeleaf page code is as follows:

Insert title here hello

test

Enter the URL: http://localhost:8090/sbe/hello/thymeleaf of the Controller that accesses the Thymeleaf page in the browser to test. The test results are as follows:

The above is about the content of this article on "how to use Thymeleaf in SpringBoot". I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please follow the industry information channel.

Tags: Templates pages content development testing code prototypes engines articles tags steps environment knowledge articles nature processing work configuration clean powerful Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi Linux OPPO Reno Shulou Tech Info Docker