How to use Thymeleaf template engine
Today, I would like to share with you the relevant knowledge about how to use the Thymeleaf template engine. The content is detailed and the logic is clear. I believe most people still know too much about this, so share this article for your reference. I hope you can get something after reading this article.
Template engines JSP, Velocity, Freemarker, Thymeleaf
Second, springboot recommends the use of Thymeleaf template engine features: simpler syntax, more powerful 1. Introduce Thymeleaf org.springframework.boot spring-boot-starter-thymeleaf 2, the use of Thymeleaf @ ConfigurationProperties (prefix = "spring.thymeleaf") public class ThymeleafProperties {private static final Charset DEFAULT_ENCODING = Charset.forName ("UTF-8"); private static final MimeType DEFAULT_CONTENT_TYPE = MimeType.valueOf ("text/html"); public static final String DEFAULT_PREFIX = "classpath:/templates/"; public static final String DEFAULT_SUFFIX = ".html" As long as we put the HTML page on classpath:/templates/,thymeleaf, it will render automatically. 3. Import the namespace of thymeleaf. 4. Use thymeleaf syntax Title successfully! This is to display welcome message 3, syntax rule 1, th:text; to change the text content in the current element; th: any html attribute; to replace the value of the native attribute
2. Expression Simple expressions: (expression syntax) Variable Expressions: ${...}: get the value of the variable; OGNL; 1), get the properties of the object, call method 2), use the built-in basic object: # ctx: the context object. # vars: the context variables. # locale: the context locale. # request: (only in Web Contexts) the HttpServletRequest object. # response: (only in Web Contexts) the HttpServletResponse object. # session: (only in Web Contexts) the HttpSession object. # servletContext: (only in Web Contexts) the ServletContext object. ${session.foo} 3), some built-in tool objects: # execInfo: information about the template being processed.#messages: methods for obtaining externalized messages inside variables expressions, in the same way as they would be obtained using # {… } syntax.#uris: methods for escaping parts of URLs/URIs#conversions: methods for executing the configured conversion service (if any). # dates: methods for java.util.Date objects: formatting, component extraction, etc.#calendars: analogous to # dates, but for java.util.Calendar objects.#numbers: methods for formatting numeric objects.#strings: methods for String objects: contains, startsWith, prepending/appending Etc.#objects: methods for objects in general.#bools: methods for boolean evaluation.#arrays: methods for arrays.#lists: methods for lists.#sets: methods for sets.#maps: methods for maps.#aggregates: methods for creating aggregates on arrays or collections.#ids: methods for dealing with id attributes that might be repeated (for example, as a result of an iteration). Selection Variable Expressions: * {...}: select expression: the function is the same as ${}; add: cooperate with th:object= "${session.user}:
Name: Sebastian.
Surname: Pepper.
Nationality: Saturn.
Message Expressions: # {...}: get internationalized content Link URL Expressions: @ {...}: define URL; @ {/ order/process (execId=$ {execId}, execType='FAST')} Fragment Expressions: ~ {...}: fragment reference expression. Literals (literally) Text literals: 'one text',' Another OnePlease', … Number literals: 0,34,3.0,12.3, … Boolean literals: true, false Null literal: null Literal tokens: one, sometext, main, … Text operations: (text operation) String concatenation: + Literal substitutions: | The name is ${name} | Arithmetic operations: (mathematical operation) Binary operators: +, -, *, /,% Minus sign (unary operator):-Boolean operations: (Boolean operation) Binary operators: and, or Boolean negation (unary operator):!, notComparisons and equality: (comparison operation) Comparators: >
< , >=