In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-12 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article is to share with you about how to use Thymeleaf in SpringBoot. The editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.
Standard expression syntax
Variable expression
Variable expressions are OGNL expressions or Spring EL expressions (also called model attributes in Spring terminology). As follows:
${session.user.name}
They will be represented by an attribute of the HTML tag:
Select (asterisk) expression
Selection expressions are much like variable expressions, except that they are executed with a pre-selected object instead of the context variable container (map), as follows:
* {customer.name}
The specified object is defined by the th:object attribute:
.........
Text internationalization expression
Text internationalization expressions allow us to get region text information (.properties) from an external file, index Value with Key, and provide a set of parameters (optional).
# {main.title} # {message.entrycreated (${entryId})}
You can find this expression code in the template file:
......
URL expression
A URL expression refers to adding a useful context or reply message to a URL, a process often referred to as URL rewriting.
@ {/ order/list}
URL can also set parameters:
@ {/ order/details (id=$ {orderId})}
Relative path:
@ {.. / documents/report}
Let's look at these expressions:
What's the difference between variable expressions and asterisk expressions?
If the context is not considered, there is no difference between the two; the asterisk syntax evaluation is expressed on the selected object, not the entire context. What is the selected object? Is the value of the parent tag, as follows:
Name: Sebastian.
Surname: Pepper.
Nationality: Saturn.
This is exactly the same as:
Name: Sebastian.
Surname: Pepper.
Nationality: Saturn.
Of course, the dollar sign and asterisk syntax can be mixed:
Name: Sebastian.
Surname: Pepper.
Nationality: Saturn.
Syntax supported by expressions
If-then: (if)? (then)
If-then-else: (if)? (then): (else)
Default: (value)?: (defaultvalue)
All of these features can be nested by group merging:
Compare (Comparators): >, =, th:if judgment condition th:unless and th:if determine the opposite Loginth:href link address Login / > th:switch Multiplexing uses a branch of th:caseth:switch User is an administrator with th:case
Th:fragment layout tag, define a code snippet to facilitate other places to reference th:include layout tag, replace content to incoming file / > th:replace layout tag, replace entire tag to incoming file th:selectedselected selection box select th:selected= "(${xxx.id} = = ${configObj.dd})" th:src picture class address introduction
The th:inline definition js script can use the variable th:action form submission address th:remove to delete an attribute 1.all: delete contains tags and all children. 2.body: does not contain tag deletion, but deletes all of its children. 3.tag: contains the deletion of the tag, but does not delete its child. 4.all-but-first: delete all children with tags except the first one. 5.none: nothing. This value is a useful dynamic evaluation. Th:attr sets tag attributes, and multiple attributes can be separated by commas, such as th:attr= "src=@ {/ image/aa.jpg}, title=# {logo}". This tag is not very elegant and is generally used less.
There are a lot of tags. Only the most commonly used ones are listed here. Because multiple th:x attributes can be included in a tag, the priority order in which they take effect is:
Include,each,if/unless/switch/case,with,attr/attrprepend/attrappend,value/href,src, etc,text/utext,fragment,remove . Several common methods of use
Assignment, string concatenation
Description
There is another concise way to write string concatenation.
Conditional judgment if/Unless
The th:if and th:unless attributes are used in Thymeleaf to determine the condition. In the following example, the tag is displayed only if the condition is established in th:if:
Login
Th:unless, in contrast to th:if, displays its contents only if the condition in the expression is not true. Can you also use (if)? (then): (else) this syntax is used to determine what is displayed
For cycle
one
Mark Otto @ mdo index
IterStat is called a state variable, and the attributes are:
-index: index of the current iteration object (calculated from 0)-count: index of the current iteration object (calculated from 1)-size: size of the object being iterated-current: current iteration variable-even/odd: Boolean value, whether the current loop is even / odd (calculated from 0)-first: Boolean value, whether the current loop is the first-last: Boolean value, whether the current loop is the last
URL
URL occupies a very important position in the Web application template, and it should be noted that Thymeleaf handles URL through the syntax @ {.}. If you need Thymeleaf to render URL, be sure to use attributes such as th:href,th:src. Here is an example
Viewview
Set background
Change the background according to the attribute value
The following points are explained:
-the last (orderId=$ {o.id}) of URL in the above example treats the contents in parentheses as URL parameters. This syntax avoids string concatenation, which greatly improves readability. The orderId variable in Context can be accessed through {orderId} in the expression. @ {/ order} is the relative path related to Context. The Context name of the current Web application is automatically added when rendering, assuming that the context name is app. Then the result should be / app/order
Inline js
Inline text: [[…]] The representation of inline text, when used, must first be activated with th:inline= "text/javascript/none". Th:inline can be used within the parent tag, or even as a body tag. Although inline text is less code than th:text, it is not conducive to prototype display.
/ * /
Js additional code:
/ * [+ var msg = 'This is a working application';+] * /
Js removal code:
/ * [- * / var msg = 'This is a non-working template';/* -] * /
Embedded variable
To make templates easier to use, Thymeleaf also provides a series of Utility objects (built into Context), which can be accessed directly through #:
Dates: the functional method class of java.util.Date. Calendars: similar to # dates, java.util.Calendarnumbers: functional method classes for formatting numbers strings: functional classes for string objects, contains,startWiths,prepending/appending, and so on. Objects: functional class operation on objects. Bools: a functional method for evaluating Boolean values. Arrays: functional class methods for arrays. Lists: setsmaps for lists functional class methods
Here is an example of some common methods with a piece of code:
Dates
/ * * Format date with the specified pattern * Also works with arrays, lists or sets * / ${# dates.format (date, 'dd/MMM/yyyy HH:mm')} ${# dates.arrayFormat (datesArray,' dd/MMM/yyyy HH:mm')} ${# dates.listFormat (datesList, 'dd/MMM/yyyy HH:mm')} ${# dates.setFormat (datesSet 'dd/MMM/yyyy HH:mm')} / * * Create a date (java.util.Date) object for the current date and time * / ${# dates.createNow ()} / * * Create a date (java.util.Date) object for the current date (time set to 00:00) * / ${# dates.createToday ()}
Strings
/ * Check whether a String is empty (or null). Performs a trim () operation before check * Also works with arrays, lists or sets * / ${# strings.isEmpty (name)} ${# strings.arrayIsEmpty (nameArr)} ${# strings.listIsEmpty (nameList)} ${# strings.setIsEmpty (nameSet)} / * * Check whether a String starts or ends with a fragment * Also works with arrays, lists or sets * / ${# strings.startsWith (name,'Don')} / / also array*, list* and set*$ {# strings.endsWith (name) EndingFragment)} / / also array*, list* and set*/* * Compute length * Also works with arrays, lists or sets * / ${# strings.length (str)} / * * Null-safe comparison and concatenation * / ${# strings.equals (str)} ${# strings.equalsIgnoreCase (str)} ${# strings.concat (str)} ${# strings.concatReplaceNulls (str)} / * * Random * / ${# strings.randomAlphanumeric (count)} III. Use Thymeleaf layout
Spring Boot 2.0 extracts the layout separately, requiring the introduction of a separate dependency: thymeleaf-layout-dialect.
Org.springframework.boot spring-boot-starter-thymeleaf nz.net.ultraq.thymeleaf thymeleaf-layout-dialect
Define code snippet
©2019
Introduce anywhere on the page:
Th:insert differs from th:replace in that insert is only loaded and replace is a replacement. Thymeleaf 3.0 recommends replacing the 2.0 th:replace with th:insert.
The returned HTML is as follows:
©2019 ©2019
The following is a commonly used background page layout, which divides the whole page into header, tail, menu bar and hidden bar. Click the menu to change only the page in the content area.
Header left sidebar footer
Any page that wants to use such a layout value only needs to replace the content module seen in
...
You can also pass parameters when referencing the template.
Layout is the address of the file. If there is a folder, it can be written in this way. FileName/layout:htmlhead,htmlhead refers to a defined code snippet such as th:fragment= "copy".
The above is how to use Thymeleaf in SpringBoot. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please 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: 236
*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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.