Expressions about Thymeleaf


What is Thymeleaf

Thymeleaf is a Java template engine that can be applied to both web projects and non-web project servers. It has the characteristics of dynamic and static combination, multi-expression support, and springboot support. Today's main purpose is to explain its expression. type of thing.
It provides a total of four expressions.


1. ${} expression

${} This can directly get the property value of the Model string, and can also get the properties of the object. You can also use . to represent the relationship of attributes.
See an example:

<div>
<p th:text="${emp.name}">
</div>

This means the name attribute of emp

Two, *{} expression

It's another simplification

<div th:object="${emp}">
<p th:text="*{name}">
</div>

You can see this example, th:object specifies the object,
and *{} can directly refer to the properties of the object, which can be optimized to some extent.

3. @{} expression

The function of this expression is to splice the path, and you will be familiar with it after reading the example.

<a th:href="@{/index}">点击</a>
<a th:href="/index">点击</a>

The effect is the same as jumping to the index, but if it is packaged into a war package, the @{} expression will add the project name to the URL, which is what you need to pay attention to.

<a th:href="@{/index(userId=1)}">点击</a>
<a th:href="/index?userId=1">点击</a>

The effect is the same, both represent a parameter with userId=1.
There is also splicing for multiple parameters, you can see the difference between using @{} and not using @{}:

<a th:href="@{/index(userId=1,age=20)}">点击</a>
<a th:href="/index?userId=1&age=20">点击</a>

It can be seen that there is a big difference between the two


4. #{} expression

This is basically not used, it is said to be used to read the properties of the internationalized properties file

Summarize

This is an essay, recording what I want to understand, and I hope it can help people who see it.
insert image description here

Supongo que te gusta

Origin blog.csdn.net/weixin_51759592/article/details/125613762
Recomendado
Clasificación