Getting started with the Standard dialects in 5 minutes

文章总结
语法:

${...} : Variable expressions.
*{...} : Selection expressions.
#{...} : Message (i18n) expressions.
@{...} : Link (URL) expressions.
~{...} : Fragment expressions.

例子:

2.1 Variable expressions

取 session 中的变量:
1. ${session.user.name}
2. 

更复杂应用:
3. 
  • 2.2 Selection expressions

    Variable expressions 类似,
    they will be executed on a previously selected object instead of the whole context variables map

    示例:

    1. *{customer.name}
    
    2. 注意使用 th:object 标签
    
    ... ... ...

    2.3 Message(i18n)expressions

    国际化示例:

    1. #{main.title}
    2. #{message.entrycreated(${entryId})}
    3. 模板中使用:
    
      ...
      
      ...
    
    ... ...
    4. 高级用法:在 message expressions 中使用 variable expressions #{${config.adminWelcomeKey}(${session.user.name})}

    2.4 Link(URL)expressions

    原文翻译:给 url 添加上 context 和 session 信息
    Link expressions are meant to build URLs and add useful context and session info to them (a process usually called URL rewriting).

    示例:
    So for a web application deployed at the /myapp context of your web server, an expression such as:

    ...
    

    Could be converted into something like this:

    ...
    

    Or even this, if we need to keep sessions and cookies are not enabled (or the server doesn’t know yet):

    ...
    

    URLs can also take parameters:

    ...
    

    Resulting in something like this:

    
    ...
    

    Link expressions can be relative, in which case no application context will be prefixed to the URL:

    ...
    

    Also server-relative (again, no application context to be prefixed):

    ...
    

    And protocol-relative (just like absolute URLs, but browser will use the same HTTP or HTTPS protocol used in the page being displayed):

    ...
    

    And of course, Link expressions can be absolute:

    ...
    

    But wait, in an absolute (or protocol-relative) URL… what value does the Thymeleaf Link Expression add? easy: the possibility of URL-rewriting defined by response filters: In a Servlet-based web application, for every URL being output (context-relative, relative, absolute…) Thymeleaf will always call the HttpServletResponse.encodeUrl(...) mechanism before displaying the URL. Which means that a filter can perform customized URL-rewriting for the application by means of wrapping the HttpServletResponse object (a commonly used mechanism).

    2.5 Fragment expressions

    最常用的两个属性:
    th:insert or th:replace

    示例:

    1. div th:insert="~{commons :: main}">...
    2.

    Fragment expressions can have arguments:

    2.5 Expression preprocessing

    格式:specified between __
    示例:#{selection.${sel.code}}

    说明(未懂):What we are seeing there is a variable expression (${sel.code}) that will be executed first and which result – let’s say, “ALL” – will be used as a part of the real expression to be executed afterwards, in this case an internationalization one (which would look for the message with key selection.ALL).

    你可能感兴趣的:(Getting started with the Standard dialects in 5 minutes)