Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the method of integrating Thymeleaf views with SpringBoot?

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces the SpringBoot integration of Thymeleaf view method is what the relevant knowledge, the content is detailed and easy to understand, the operation is simple and fast, with a certain reference value, I believe that everyone after reading this SpringBoot integration of Thymeleaf view method is what the article will have a harvest, let's take a look.

Introduction to Thymeleaf View

Let's first take a look at the introduction on the official website:

= = 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 can be displayed correctly in browsers or work as a static prototype to enhance collaboration among development teams.

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

In SpringBoot, SpringBoot provides good support for Thymeleaf as well as automatic configuration, so using Thymeleaf in SpringBoot is very fast and convenient.

Create a SpringBoot project

For the creation method, it is recommended to use IDEA to quickly create SpringBoot projects, and select web and Thymeleaf dependencies

After the creation is completed, IDEA automatically adds web and Thymeleaf dependency management to pom, pom.xml:

Org.springframework.boot spring-boot-starter-thymeleaf org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test test configuration Thymeleaf

SpringBoot provides automatic configuration class ThymeleafAutoConfiguration for Thymeleaf, source code:

@ Configuration@EnableConfigurationProperties ({ThymeleafProperties.class}) @ ConditionalOnClass ({TemplateMode.class, SpringTemplateEngine.class}) @ AutoConfigureAfter ({WebMvcAutoConfiguration.class, WebFluxAutoConfiguration.class}) public class ThymeleafAutoConfiguration {...}

You can see that the relevant configuration information is obtained from the ThymeleafProperties class. Take a closer look at the source code of ThymeleafProperties:

@ ConfigurationProperties (prefix = "spring.thymeleaf") public class ThymeleafProperties {private static final Charset DEFAULT_ENCODING; public static final String DEFAULT_PREFIX = "classpath:/templates/"; public static final String DEFAULT_SUFFIX = ".html"; private boolean checkTemplate = true; private boolean checkTemplateLocation = true; private String prefix = "classpath:/templates/"; private String suffix = ".html"; private String mode = "HTML"; / / omitted}

From this configuration, we can see that the default storage location of Thymeleaf is under classpath:/templates/, that is, resources/templates/. This directory has been automatically generated when we created the project using IDEA.

If we need to make changes to the configuration of Thymeleaf, we can configure it directly in application.properties:

# whether to enable caching. Default is truespring.thymeleaf.cache=false# check whether template file exists spring.thymeleaf.check-template=true# check template directory whether there is spring.thymeleaf.check-template-location=true# template file encoding spring.thymeleaf.encoding=UTF-8# template location spring.thymeleaf.prefix=classpath:/templates/# template file suffix spring.thymeleaf.suffix=.html#Content-typespring.thymeleaf.servlet.content-type=text/html write Demo

1. Create User and UserController:

User.java:

Package com.gongsir.springboot02.pojo;public class User {private String name; private String major; private String grade; public String getName () {return name;} public void setName (String name) {this.name = name;} public String getMajor () {return major;} public void setMajor (String major) {this.major = major } public String getGrade () {return grade;} public void setGrade (String grade) {this.grade = grade;}}

UserController.java:

@ Controllerpublic class UserController {@ GetMapping (path = "/ users") public ModelAndView getUsers () {List list = new ArrayList (); User U1 = new User (); u1.setName ("Gong Tao"); u1.setMajor ("computer"); u1.setGrade ("2017"); list.add (U1); User U2 = new User (); u2.setName ("Li Shiya") U2.setMajor ("network engineering"); u2.setGrade ("2017"); list.add (U2); / / the name of the view template file. Create a template file with the same name ModelAndView mv = new ModelAndView ("users"); mv.addObject ("users", list); return mv;}} in the template directory.

2. Create a new users.html template file under the template directory to display the data:

User list name professional grade

3. Start the project and visit http://localhost:8080/users

This is the end of the article on "how SpringBoot integrates Thymeleaf views". Thank you for reading! I believe that everyone has a certain understanding of the knowledge of "what is the method of SpringBoot integrating Thymeleaf view". If you want to learn more knowledge, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report