Thymeleaf syntax summary

What Thymeleaf that?

   1.Thymeleaf Jieke run under the environment of network and non-network that it allows artists to see the effect of static page in a browser, but also allows the programmer to see the effect of dynamic pages with data on the server. This is because it supports html prototype, and then add additional attributes to achieve in the html tag template + display of the data. The browser will ignore the interpretation of undefined html tag attributes, so thymeleaf templates can be statically run; when there is data to return to the page, Thymeleaf label dynamically replace static content, dynamic display the page.

    2.Thymeleaf-use out of the box characteristics. It provides standard and spring standard two dialects, can directly apply a template to achieve JSTL, OGNL expressions effect, avoid sets per day template, troubled by the jstl, change the label. At the same time developers can extend and create custom dialect.

    3. Thymeleaf standard dialect and provide a perfect spring and SpringMVC integrated optional modules, you can quickly achieve a form of binding, property editor, international and other functions.

   Common attributes:

A, th property

Common th property interpretation

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.

A, TH: text  : Set the text content of the current element, as well as the same function th: utext difference between the two is that the former does not escape html tags, the latter will be. Priority is not high: order = 7

Two, TH: value : set the value of the current element, as well as properties similar modifications specified TH: the src , TH: the href . Priority is not high: order = 6

Three, th: each : through the loop element, and th: text or th: use with value. Note the location of the property modified label, detailed look back. A high priority: order = 2

Four, TH: IF : condition judgment, there are similar TH: The unless , TH: Switch , TH: Case . Higher priority: order = 3

Five, 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, commonly used in public code blocks extracted scene. The highest priority: order = 1

Six, TH: the fragment : custom code block is easy th: insert reference. Lowest priority: order = 8

Seven, TH: Object : declaration of variables, and is generally used in conjunction with * {}, lazy achieve the effect. General priority: order = 4

Eight, th: attr : modify any property, with less actual development, because there are a wealth of other th property to help, there are similar th: attrappend, th: attrprepend. General priority: order = 5

Common use th property

Use Thymeleaf attribute points to note the following five points:

First, to use Thymeleaf grammar, you must first declare a namespace: xmlns:th="http://www.thymeleaf.org"

Second, set the text th: text, set value input of th: value, output cycle th: each, conditional th: if, into code blocks th: insert, defined block th: fragment, declare a variable th: object

Three, th: each usage need extra attention, an analogy: if you want to loop in a div tag p, then th: each property must be placed on the p tag. If you would th: each attribute on div, the loop is to the whole div.

Fourth, the variable expression provides many built-in method, the method is to begin with a built-# Please do not confuse the message expression} # {.

Five, th: insert, th: replace, th: three kinds into code blocks include a similar effect, but very different.

<! DOCTYPE HTML > 
<-! Namespace -> 
< HTML lang = "EN" xmlns: TH = "http://www.thymeleaf.org" > 
< head > 
    < Meta charset = "UTF-. 8" > 
    < title > Thymeleaf syntax </ title > 
</ head > 
< body > 
    < H2 > ITDragon Thymeleaf syntax </ H2 > 
    <-! TH: set the current text of the text element, commonly, the priority is not high -> 
    <p th:text="${thText}" /> 
    < P TH: UTEXT = "$ {thUText}"  /> 

    <-! TH: Set value value value of the current element, commonly, only priority than th: text High -> 
    < INPUT type = "text" TH: value = "$ {thValue}"  /> 

    <-! TH: each traversal list, common, high priority, this is inserted in the code block only -> 
    <-! TH: each modification on div , the repeated layer div, p tag if just traversed, the tag is modified on the p -> 
    < div TH: each = "Message: thEach $ {}" >  ! <- through the entire p-div, not recommended -> 
        < P TH: text = "$} {Message"  /> 
    </ div > 
    <div> <!--Traverses only the p, recommended -> 
        < P TH: text = "$} {Message" TH: each = "Message: thEach $ {}"  /> 
    </ div > 

    <-! TH: IF condition judgment, similar there th: switch, th: case, after the priority th: each, which is a built-in method #strings variable expression -> 
    < P TH: text = "$ {} THIF" TH: IF = "$ Not strings.isEmpty # {(THIF)} " > </ P > 

    <-! TH: iNSERT div current block into the code, the highest priority, there is a similar th: replace, th: include, ~ {}: expressions block -> 
    < div TH: INSERT = "~ {Grammar / Common thCommon} ::" > </ div > 

    <!- TH: Object declare variables, {*} and used together ->
    <div th:object="${thObject}">
        <p>ID: <span th:text="*{id}" /></p><!--th:text="${thObject.id}"-->
        <p>TH: <span th:text="*{thName}" /></p><!--${thObject.thName}-->
        <p>DE: <span th:text="*{desc}" /></p><!--${thObject.desc}-->
    </div>

</body>
</html>

Controller level code

import com.itdragon.entities.ThObject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Controller
public class ThymeleafController {

    @RequestMapping("thymeleaf")
    public String thymeleaf(ModelMap map) {
        map.put("thText", "th:text 设置文本内容 <b>加粗</b>");
        map.put("thUText", "th: utext provided text <b> bold </ B>" ); 
        map.put ( "thValue", "thValue set value of the current element" ); 
        map.put ( "thEach", Arrays.asList ( "th: each", " traversing the list" )); 
        map.put ( "THIF", "msg IS not null" ); 
        map.put ( "thObject", new new thObject (1L, "TH: Object "," th attribute for lazy " ));
         return " Grammar / Thymeleaf " ; 
    } 
}

Original link: https://www.cnblogs.com/itdragon/archive/2018/04/13/8724291.html

 

Guess you like

Origin www.cnblogs.com/420ITboy/p/12509138.html