Detailed SpringBoot (2): thymeleaf

# Spring Boot detailed explanation (2): thymeleaf

does not recommend the use of jsp because of Spring Boot, probably because of the poor readability of jsp. The readability of thymeleaf is very good. At first glance, it feels like an html page. The effect can also be displayed through the browser without the need for a web server. Similar to thymeleaf, there is also freemarker, which is currently the two most used template engines.

## 1. Configuration

~~~
#thymeleaf
spring.thymeleaf.suffix=.html
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.mode=HTML5
spring.thymeleaf.servlet .content-type=text/html
~~~

spring.thymeleaf.suffix=.html means that the page suffix is ​​in html format

spring.thymeleaf.prefix=classpath:/templates/ means that the html page is in the templates folder under the resources folder.

spring.thymeleaf.encoding=UTF-8 Use utf-8 encoding to avoid garbled pages.

spring.thymeleaf.mode=HTML5 The page is of html5 type.

spring.thymeleaf.servlet.content-type=text/html The document MIME type is text/html, which is the document in html format.



### 1.1. Basic syntax

~~~
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Thymeleaf template</title>
    <meta http-equiv ="Content-Type" content="text/html; charset=UTF-8"/>
</head>
<body>


<p th:text="${msg}">Hello World</p>

</body >
</html>
~~~

The overall syntax is the same as html, just pay attention to this location <html xmlns:th="http://www.thymeleaf.org">.

## 2. Value

~~~
<p th:text="${msg}">Hello World</p>
~~~

As above, get the msg parameter passed by the back-end interface. Basically all html tags, except th: text="". th:text is the thymeleaf template tag used to display the data passed by the backend and replace the Hello World text. The above sentence means that if msg has a value, the msg content will be displayed, otherwise, Hello World will be displayed.



### 2.1. Introduce url

~~~
<a th:href="@{http://javen666.com}">Class Notes</a>
~~~



## 3. Conditional branch

### 3.1. Condition

th:if if the condition is true

th:unless if the condition is not true For

example :

~~~
<a th:href="@{/login}" th:unless=${session.user != null}>Login</a>
~ ~~

### 3.2. Branch

th:switch

such as:

~~~
<div th:switch="${user.role}">
  <p th:case="'admin'">User is an administrator</p >
  <p th:case="#{roles.manager}">User is a manager</p>
</div>
~~~



## 4. Loop

th:each For

example:

~~~~
<tr th:each ="prod : ${prods}">
      <td th:text="${prod.name}">Onions</td>
      <td th:text="${prod.price}">2.41</td>
      <td th:text="${prod.inStock}? #{true} : #{false}">yes</td>
    </tr>
~~~~



## 5.

The +-*/ operation can be performed in the ${ } expression.



## 6.

In addition to the most basic operations provided by other thymeleaf, it also has certain support for string strings, date formatting, set null judgment, etc. For example:

~~~
${#dates.format(date, 'dd/MMM/yyyy HH:mm')}

${#strings.isEmpty(name)}
~~~



Note: thymeleaf does not have to be in the spring boot environment to be used. Generally springmvc integration thymeleaf can also be used, the usage is the same.

Guess you like

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