SpringBoot integrates with Thymeleaf

Thymeleaf 网 网

http://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html

Static resource access

When we develop web applications, we need to reference a large number of static resources such as js, css, and pictures.

default allocation

By default, Spring Boot provides a static resource directory location that needs to be placed on the classpath, and the directory name must meet the following rules:

  • /static
  • /public
  • /resources
  • /META-INF/resources

Example: We can create static in the src/main/resources/ directory and place an image file in this location. After starting the program, try to visit http://localhost:8080/test.jpg . If the picture can be displayed, the configuration is successful.

template engine

Spring Boot is still perfectly competent in the implementation of dynamic HTML, and provides default configuration support for a variety of template engines, so under the recommended template engine, we can quickly start developing dynamic websites.

The main types of template engines that Spring Boot provides by default are as follows:

  • Thymeleaf
  • FreeMarker
  • Velocity
  • Groovy
  • Mustache

Spring Boot recommends using these template engines and avoiding the use of JSP. If you must use JSP, you will not be able to implement various features of Spring Boot

When you use any of the above template engines, their default template configuration path is: src/main/resources/templates. Of course, this path can also be modified. How to modify it can be queried and modified in the configuration properties of subsequent template engines.

Thymeleaf

Thymeleaf is an XML/XHTML/HTML5 template engine that can be used for application development in web and non-web environments. It is an open source Java library, licensed under the Apache License 2.0, created by Daniel Fernández, the author of the Java encryption library Jasypt.

Thymeleaf provides an optional module for integrating Spring MVC. In application development, you can use Thymeleaf to completely replace JSP or other template engines, such as Velocity, FreeMarker, etc. The main goal of Thymeleaf is to provide a way to create a well-formed template that is displayed correctly by the browser, and thus can also be used for static modeling. You can use it to create validated XML and HTML templates. Rather than writing logic or code, developers can simply add tag attributes to the template. These tag attributes then execute pre-defined logic on the DOM (Document Object Model).
Example:

<table>
  <thead>
    <tr>
      <th th:text="#{msgs.headers.name}">Name</td>
      <th th:text="#{msgs.headers.price}">Price</td>
    </tr>
  </thead>
  <tbody>
    <tr th:each="prod : ${allProducts}">
      <td th:text="${prod.name}">Oranges</td>
      <td th:text="${#numbers.formatDecimal(prod.price,1,2)}">0.99</td>
    </tr>
  </tbody>
</table>

It can be seen that Thymeleaf is mainly added to the html tag in the form of attributes. When the browser parses the html, it will ignore the attributes that are not found, so the Thymeleaf template can be directly opened and displayed through the browser, which is very beneficial to the front and back ends. separation.
To use Thymeleaf in Spring Boot, you only need to introduce the following dependencies and src/main/resources/templateswrite a template file in the default template path.

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

After completing the configuration, give a simple example, based on the quick start project, give a simple example to render a page through Thymeleaf.

@RestController
public class HelloController {
    @RequestMapping("/")
    public String index(ModelMap map) {
        // 加入一个属性,用来在模板中读取
        map.addAttribute("host", "http://blog.didispace.com");
        // return模板文件的名称,对应src/main/resources/templates/index.html
        return "index";  
    }
}
<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8" />
    <title></title>
</head>
<body>
<h1 th:text="${host}">Hello World</h1>
</body>
</html>

As shown in the above page, directly open the html page to display Hello World, but after starting the program, the access http://localhost:8080/will display the value of host in the Controller: http://blog.didispace.com, which achieves the logical separation of data without destroying the content of HTML itself.
Thymeleaf's default parameter configuration:
If you need to modify the default configuration, just copy the following properties to application.properties and modify them to the required values, such as modifying the extension of the template file, modifying the default template path, etc. .

# Enable template caching.
spring.thymeleaf.cache=true 
# Check that the templates location exists.
spring.thymeleaf.check-template-location=true 
# Content-Type value.
spring.thymeleaf.content-type=text/html 
# Enable MVC Thymeleaf view resolution.
spring.thymeleaf.enabled=true 
# Template encoding.
spring.thymeleaf.encoding=UTF-8 
# Comma-separated list of view names that should be excluded from resolution.
spring.thymeleaf.excluded-view-names= 
# Template mode to be applied to templates. See also StandardTemplateModeHandlers.
spring.thymeleaf.mode=HTML5 
# Prefix that gets prepended to view names when building a URL.
spring.thymeleaf.prefix=classpath:/templates/ 
# Suffix that gets appended to view names when building a URL.
spring.thymeleaf.suffix=.html  spring.thymeleaf.template-resolver-order= # Order of the template resolver in the chain. spring.thymeleaf.view-names= # Comma-separated list of view names that can be resolved.

Guess you like

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