打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

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

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";    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

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 />
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

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

==================== 动态生成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 />
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

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

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

3. 代码

详细代码见Github

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
Thymeleaf 的使用
Thymeleaf-基础
thymeleaf 学习笔记
IEnumerable<T>和IQueryable<T>区分
iOS中Objective
C# 中正则表达式 Group 分组
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服