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

来源:互联网 发布:台州五轴编程工资 编辑:程序博客网 时间:2024/06/06 03:01

1. 概述

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

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

2. 主代码

2.1. 公共代码

ExpressionsCtl:Control类

@Controller@RequestMapping("/expressions")public class ExpressionsCtl {    /**     * 处理URL     *      * @param modeMap     * @return     */    @RequestMapping("/url")    public String url(ModelMap modeMap){        modeMap.put("id", "123");        return "expressions/url";    }    /**     * 表达式工具对象:Expression Utility Objects     *      */    @RequestMapping("/utility")    public String utility(ModelMap modeMap){        return "expressions/utility";    }}

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

2.2. 生成URL地址@{}

演示如下功能

  • th:href生成的值替换的href值 @{}
  • url中加入变量值(orderId=${id})做为url的请求参数
==================== 动态生成URL ===============================<br/><!-- th:href生成的值替换<a>的href值; (orderId=${id})做为url的请求参数 -->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 /><!-- 生成:/order/details?orderId=123 -->/order/details?orderId=123 --> <a href="details.html" th:href="@{/order/details(orderId=${id})}">view</a> <br /><!-- 替换url中变量值,生成/order/123/details -->/order/123/details --> <a href="details.html" th:href="@{/order/{orderId}/details(orderId=${id})}">view</a> <br />

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

==================== 动态生成URL ===============================<br /><!-- th:href生成的值替换<a>的href值; (orderId=${id})做为url的请求参数 -->http://localhost:8080/gtvg/order/details?orderId=123 --> <a href="http://localhost:8080/gtvg/order/details?orderId=123">view</a> <br /><!-- 生成:/order/details?orderId=123 -->/order/details?orderId=123 --> <a href="/order/details?orderId=123">view</a> <br /><!-- 替换url中变量值,生成/order/123/details -->/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的用法,其它工具类用法见官方的用法详细见这里,

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

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

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

3. 代码

详细代码见Github

原创粉丝点击