spring集成Thymeleaf,模板页面中调用类的静态成员

xiaoxiao2021-02-28  26

https://blog.csdn.net/smartcore/article/details/79994752

在spring框架内使用Thymeleaf时,按照官方文档的说明,在模板内会使用SpEL(spring expressionlanguage)代替OGNL,即可以使用前者约定的一系列表达式。

关于SpEL的说明,已经有很多的说明,官方的可见此页面。本文是对其中“T”操作符(说明见此)实践的示例,由T()表达式的使用,我们使用Thymeleaf模板引擎时,在html页面中能够使用类的静态成员。

如以下的代码内,示例了在<input>标签、js代码段内,对类的静态方法的引用(当然,方法应该是public的),需要注意的是:按照文档说明,大部分情况下,需要是完整的类名。

[html]  view plain  copy <!DOCTYPE html>  <html lang="zh-cmn-Hans" xmlns:th="http://www.thymeleaf.org">  <head>      <meta charset="UTF-8"/>      <title>T操作符-引用类的静态成员</title>  </head>  <body>      <!-- 标签内引用 -->      <div>          <input type="text" th:value="${T(java.util.Calendar).getInstance().getTime().toLocaleString()}" />      </div>  </body>  <script type="text/javascript" th:inline="javascript">      /*<![CDATA[*/     var g_strCurrentTime = /*[[${T(java.util.Calendar).getInstance().getTime().toLocaleString()}]]*/ '';     /*]]>*/  </script>    </html>  

对类的静态成员变量,其引用方式与上面的静态方法也是一样的。常量的引用:

<div>          <input type="text" th:value="${T(com.lyl.constants).DATE_FORMAT_PATTERN}" />      </div>  

转载请注明原文地址: https://www.6miu.com/read-2450170.html

最新回复(0)