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
阅读全文
0 0
- Thymeleaf系列四 生成URL地址和表达式工具对象
- Thymeleaf URL表达式
- thymeleaf自定义工具对象
- ThymeLeaf通过模板转换对象(根据模板和map生成)
- Thymeleaf教程 (四) Thymeleaf标准表达式语法(上)
- Thymeleaf教程 (四) Thymeleaf标准表达式语法(上)
- Thymeleaf系列二 简单表达式: 变量、消息、Literals、文本、算术、比较和条件表达式
- Spring Boot系列(四):thymeleaf使用详解
- Spring Boot系列(四):thymeleaf使用详解
- Thymeleaf条件表达式和默认表达式
- 通过图片url生成Bitmap对象和Drawable对象
- 通过图片url生成Bitmap对象和Drawable对象
- Spring boo系列--jpa和thymeleaf
- URL地址验证正则表达式
- Spring Boot干货系列:(四)Thymeleaf篇
- 使用正则表达式自动为文本中的url地址生成链接
- 使用正则表达式自动为文本中的url地址生成链接
- Thymeleaf教程 (十三) 附件:工具对象用法
- linux環境下掃描文件
- linux GCC与Vi编辑器
- Angular项目目录介绍
- 图像识别
- iOS应用审核
- Thymeleaf系列四 生成URL地址和表达式工具对象
- 海量数据高并发的访问技术解决方案
- leetcode287. Find the Duplicate Number
- WebSocket + Spring消息推送服务的快速实现
- 使用GPU
- Failover与Failback的区别
- 微服务架构的两大解耦利器与最佳实践
- 计算机相关岗位要求
- Nginx状态信息(status)配置及信息详解