Thymeleaf系列四 生成URL地址和表达式工具对象

x33g5p2x  于2021-12-24 转载在 其他  
字(4.1k)|赞(0)|评价(0)|浏览(391)

1. 概述

本节的标准表达式语法包含如下内容:

  • 生成URL地址:@{}
  • 表达式工具对象:Expression Utility Objects

2. 主代码

2.1. 公共代码

ExpressionsCtl:Control类

  1. @Controller
  2. @RequestMapping("/expressions")
  3. public class ExpressionsCtl {
  4. /** * 处理URL * * @param modeMap * @return */
  5. @RequestMapping("/url")
  6. public String url(ModelMap modeMap){
  7. modeMap.put("id", "123");
  8. return "expressions/url";
  9. }
  10. /** * 表达式工具对象:Expression Utility Objects * */
  11. @RequestMapping("/utility")
  12. public String utility(ModelMap modeMap){
  13. return "expressions/utility";
  14. }
  15. }

url()方法转到url.html, 包含urls的用法代码; utility()方法转到utility.html,包含表达式工具对象相关代码。

2.2. 生成URL地址@{}

演示如下功能

  • th:href生成的值替换
  • url中加入变量值(orderId=${id})做为url的请求参数
  1. ==================== 动态生成URL ===============================<br/>
  2. <!-- th:href生成的值替换<a>的href值; (orderId=${id})做为url的请求参数 -->
  3. http://localhost:8080/gtvg/order/details?orderId=123 --> <a href="details.html" th:href="@{http://localhost:8080/gtvg/order/details(orderId=${id})}">view</a> <br />
  4. <!-- 生成:/order/details?orderId=123 -->
  5. /order/details?orderId=123 --> <a href="details.html" th:href="@{/order/details(orderId=${id})}">view</a> <br />
  6. <!-- 替换url中变量值,生成/order/123/details -->
  7. /order/123/details --> <a href="details.html" th:href="@{/order/{orderId}/details(orderId=${id})}">view</a> <br />

输出: “–>”的左边是语法,右边是对应的输出(这里以源码方式显示)

  1. ==================== 动态生成URL ===============================<br />
  2. <!-- th:href生成的值替换<a>的href值; (orderId=${id})做为url的请求参数 -->
  3. http://localhost:8080/gtvg/order/details?orderId=123 --> <a href="http://localhost:8080/gtvg/order/details?orderId=123">view</a> <br />
  4. <!-- 生成:/order/details?orderId=123 -->
  5. /order/details?orderId=123 --> <a href="/order/details?orderId=123">view</a> <br />
  6. <!-- 替换url中变量值,生成/order/123/details -->
  7. /order/123/details --> <a href="/order/123/details">view</a> <br />

2.3. 表达式工具对象:Expression Utility Objects

thymeleaf有很多工具类,所有工具类如下:

  • #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).

这里只列出#messages,#uris的用法,其它工具类用法见官方的用法详细见这里,

  1. ==================== #messages ===============================<br/>
  2. <!-- #messages加载外部属性文件的key,对应接口org.thymeleaf.expression.Messages 这里只列出一种用法,其他可以用法也是类似 详细见:http://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html#messages-1 -->
  3. ${#messages.msgOrNull('home.welcome.replace','message')} --> <span th:text="${#messages.msgOrNull('home.welcome.replace','message')}" ></span>
  4. <br />
  5. ==================== #uris ===============================<br/>
  6. <!-- #uris: URL/URI的工具类,对应接口org.thymeleaf.expression.Uris 详细见: http://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html#urisurls -->
  7. ${#uris.escapePath('http://blog.csdn.net/hry2015/')} --> <span th:text="${#uris.escapePath('http://blog.csdn.net/hry2015/')}" ></span>

输出: “–>”的左边是语法,右边是对应的输出

  1. ==================== #messages ===============================
  2. ${#messages.msgOrNull('home.welcome.replace','message')} --> welcome thymeleaf, message!
  3. ==================== #uris ===============================
  4. ${#uris.escapePath('http://blog.csdn.net/hry2015/')} --> http://blog.csdn.net/hry2015/

3. 代码

详细代码见Github

相关文章