本节的标准表达式语法包含如下内容:
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,包含表达式工具对象相关代码。
演示如下功能
==================== 动态生成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 />
thymeleaf有很多工具类,所有工具类如下:
这里只列出#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/
详细代码见Github
联系客服