SpringBoot tenth chapter: thymeleaf Comments

Author: Dream 1819
Original: https: //www.cnblogs.com/yanfei1819/p/10931435.html
Copyright: This article is a blogger original article, reproduced, please attach Bowen link!

introduction

  SpringBoot support for the Web, the official recommended that the template engine thymelaf. In this chapter, the authors explain the intent is directly integrated with Thymelaf of SpringBoot. But in the example, we find that integration is not difficult. The core concern should be the use of Thymelaf template engine itself. Therefore, this chapter will be singled out to explain it. Detailed be an integral part of the next chapter.


Brief introduction

  Thymeleaf is a tool for rendering XML / XHTML / HTML5 content template engine. Similarly JSP, Velocity, FreeMaker, etc., it can easily be integrated template engine as a Web application and Web frameworks such as Spring MVC. Compared with other template engines, Thymeleaf biggest feature is the ability to open directly in the browser and displays the correct page template without the need to start the entire Web application. Achieve a stronger coordinating role in the development team.


Features

  • That prototype pages;

  • Internationalization support;

  • Out of the box;

  • Seamless integration with Spring.


Standard expression syntax

1, ${...}variable expression, Variable Expressions

Variable expression can use built-in objects and built-in method of thymleaf. E.g:

<p>Today is: <span th:text="${today}">13 february 2011</span>.</p>

2, @{...}link expressions, Link URL Expressions

Static resource references, links and other form request form. E.g:

无参:@{/xxx}

有参:@{/xxx(k1=v1,k2=v2)} 对应url结构:xxx?k1=v1&k2=v2

引入本地资源:@{/项目本地的资源路径}

引入外部资源:@{/webjars/资源在jar包中的路径}

3, #{...}message expression, Message Expressions

Information expressions commonly used for internationalization.

4, ~{...}block the expression, Fragment Expressions

  Code expression is mainly used for page layout, supports two grammatical structure:

  • recommend:~{templatename::fragmentname}
  • stand by:~{templatename::#id}

note:

  1. templatename: template name, Thymeleaf based on the template the full path name parsing: /resources/templates/templatename.html, pay attention to the path of the file;
  2. fragmentname: fragment name, Thymeleaf by th: fragment declaration defines the code block, i.e.,: th:fragment="fragmentname";
  3. id: HTML is id selector, when used to add the # sign in front, do not support the class selector;
  4. Th code block expression requires complex property (th: insert, th: replace, th: include) use with:

   th: insert : The entire fragment is inserted into a code block using th: insert the HTML tags;

   th: replace : replace the entire code block segments using th: replace the HTML tags;

   th: include : content block comprising a fragment inserted into using th: include the HTML tags.

5. *{...}Select (asterisk) variable expression, Selection Variable Expressions

  Select expression variables like expressions, but they use a pre-selected target instead of a context variable container (Map) is performed.

  Variable expression most frequently used, its function is also very rich. So we start with a simple block of code expression starts, then the message expression, the expression is the link again, and finally the variable expression, comes to introduce a selection variable expression.


Built-in objects and methods

1, built-in objects

  • ctx : context object.
  • VARS : context variables.
  • locale : locale context.
  • Request :( only objects in the HttpServletRequest web context) of.
  • the Response :( only HttpServletResponse objects in the web context) of.
  • the session :( only HttpSession object in the web context) of.
  • servletContext :( only the ServletContext objects in the web context) of.

2, built-in method

  • strings : string formatting method, it has a regular Java methods. For example: equals, equalsIgnoreCase, length, trim , toUpperCase, toLowerCase, indexOf, substring, replace, startsWith, endsWith, contains, containsIgnoreCase the like;

  • Numbers : Numerical formatting methods, commonly used methods are: formatDecimal the like;

  • bools : Boolean methods, commonly used methods are: isTrue, isFalse the like;

  • Arrays : Array method, commonly used methods are: toArray, length, isEmpty, contains , containsAll the like;

  • Lists , sets : a set of methods, commonly used methods are: toList, size, isEmpty, contains , containsAll, sort the like;

  • Maps : object method, commonly used methods are: size, isEmpty, containsKey, containsValue the like;

  • a dates : the date method, commonly used methods are: format, year, month, hour , createNow and so on.


Expression syntax support

Literal (Literals)

  • Text Text (Text literals): 'one text', 'Another one!',…
  • Digital text (Number literals): 0, 34, 3.0, 12.3,…
  • Boolean text (Boolean literals):true, false
  • Empty (Null literal):null
  • Word mark (Literal tokens):one, sometext, main,…

Operation text (Text operations)

  • String concatenation (String concatenation):+
  • Replace Text (Literal substitutions):|The name is ${name}|

Arithmetic (Arithmetic operations)

  • Binary operator (Binary operators):+, -, *, /, %
  • Minus (unary) Minus sign (unary operator):-

Boolean operations (Boolean operations)

  • Binary operator (Binary operators):and, or
  • Boolean negation (unary) Boolean negation (unary operator):!, not

Comparison and equivalents (Comparisons and equality)

  • Compare (Comparators):>, <, >=, <= (gt, lt, ge, le)
  • Equivalence operator (Equality operators):==, != (eq, ne)

Conditional operator (Conditional operators)

  • If-then:(if) ? (then)
  • If-then-else:(if) ? (then) : (else)
  • Default: (value) ?:(defaultvalue)


Common th property

  Some html attributes, Thymeleaf basically have, but common attributes about seven or eight. Wherein th priority attribute from 1 to 8 performed, the higher the number the lower the priority.

  • TH: text : Set the text content of the current element, there are the same functions th: utext difference between the two is that the former does not escape html tags, the latter will be. Not high priority: order = 7;
  • TH: value : set the value of the current element, as well as properties similar modifications specified TH: the src , TH: the href . Not high priority: order = 6;
  • th: each : through the loop element, and th: use with value: text or th. Note the location of the property modified label, detailed look back. A high priority: order = 2;
  • TH: IF : condition judgment, there are similar TH: The unless , TH: Switch , TH: Case . Higher priority: order = 3;
  • TH: INSERT : introducing a code block, there are similar TH: Replace , TH: the include , the difference between the three large, if the inappropriate use will destroy html structure, used in the common scene extraction block. The highest priority: order = 1;
  • TH: the fragment : custom code block is easy th: insert reference. Lowest priority: order = 8;
  • TH: Object : declaration of variables, and is generally used in conjunction with * {}, lazy achieve the effect. General priority: order = 4;
  • th: attr : modify any property, less used in the actual development, because there are a wealth of other th property to help, there are similar th: attrappend, th: attrprepend. General priority: order = 5


to sum up

  Overall, the use of thymelaf is still very simple.

  This article referring to the thymelaf official document , listed are some common usage or syntax. Of course, if the reader's ability to read English well, recommended official documents read through it, it will be clearer at the minutiae. On the other hand, if relatively strong base of readers, the authors recommend to read thymelaf source. Template engine, in fact, the general principles are similar. Here limited space, do not do in-depth explanation.

  If you have time to follow-up, the authors study together with you, to share the source code series.

reference

   thymelaf official documents



Guess you like

Origin www.cnblogs.com/yanfei1819/p/10931435.html