Use of thymeleaf templates

Source: Internet
Author: User

Thymeleaf is a template engine similar to Velocity and Freemarker, which can completely replace JSP.
Compared with other template engines, it has the following three attractive features:

1.Thymeleaf can be run in both a network and a network-free environment, which allows the artist to view the static effect of the page in the browser, or to allow programmers to view dynamic page effects with data on the server. This is because it supports HTML prototypes, and then adds additional attributes to the HTML tag to show how the template + data is presented. When the browser interprets HTML, the undefined label attribute is ignored, so the thymeleaf template can be run statically, and when data is returned to the page, the thymeleaf tag dynamically replaces the static content, making the page dynamic.

2.Thymeleaf out-of-the-box features. It provides standard and spring standard dialect, can directly apply the template to achieve jstl, OGNL expression effect, avoid the daily set of templates, the Jstl, change the label of the puzzle. Developers can also expand and create custom dialects.

3. Thymeleaf provides a spring standard dialect and an optional module that is perfectly integrated with the SPRINGMVC, enabling quick implementation of form bindings, attribute editors, and internationalization.
Standard expression Syntax

1. Simple Expressions

   ${...}  Variable expression

*{...}  Select variable expression

#{...}  Message expression

@{...}  Link-url expression

2. Literal

' One text ', ' another one! ',...   Text

0,34,3.0,12.3,... Value

True false Boolean type

null null
   one,sometext,main literal character  

3. Text Manipulation

+  String Connection

| The name is ${name}|  String connection

4. Arithmetic Operations

+,-, *,/,%  two operator

-  minus sign (unary operator)

5. Boolean Operations

And,or  $ Two operator

!,not non (unary operator)

6. Relational operators

>, <, >=, <= (GT, lt, GE, le)

= =,! = (eq, ne)

7. Conditional Judgment

· Conditional Judgment

(IF)? (then)      If-then

(IF)? (then): (Else)   If-then-else  

thymeleaf Value

Values saved in model:

The Thymeleaf template value is similar to the use of JSTL in JSP.

1. Value of the general variable:

 

2. Take the value from the object:

<input type= "text" name= "name" value= "James Carrot" th:value= "${user.name}"/>

3,list Loop Iteration

<ul>
        <li th:each= "item: ${list}" th:text= "${item}" ></li>
  </ul>

4, select/asterisk expression { *}

<div th:object= "${session.user}" >                                                                         
     <p>nationality: <span th:text= "*{nationality}" >Saturn< /span>.</p>      
</div>

The selection expression is typically followed by the Th:object, which takes the attributes in the object directly

5.URL-expression

<a href= "details.html" th:href= "@{/order/details (Orderid=${o.id})}" >view</a>
  

6, number, date

    <dt> Price </dt>  
  <dd th:text= "${#numbers. Formatdecimal (Product.price, 1, 2)}" >180</dd>  
<dt> Arrival Date </dt>  

7, string concatenation

<dd th:text= "${' $ ' +product.price}" >235</dd>  

8. Conditional Judgment

<span th:if= "${product.price LT" class= "offer" >special offer!</span>

Can not use "<", ">" and other symbols, to use "LT" and other alternatives

<!--when gender exists, select the corresponding option, and if gender does not exist or is null, obtain the name-->  
<td th:switch= "${customer.gender" of the Customer object. Name ()} ">  
     <!--use"/images/male.png "image--  
    < IMG th:case= "' FEMALE '" src= ". /.. /.. /images/female.png "th:src=" @{/images/female.png} "alt=" female "/> <!--use"/images/female.png "image--  
    <span th:case= "*" >Unknown</span>  
</td>  
<!--increase class= "enhanced" when balance heavy rain 10000--  

9.js Fetch Value

<script th:inline= "JavaScript" >/ 
 *    Verify that the introduced jquery-3.3.1.min.js is valid */
/*<![ cdata[*/  

    var name = [[${name}]];  
    alert (name);

/*]]>*/  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.