Spring + Thymeleaf 方言

版权声明:欢迎转载,但请保留出处,谢谢 https://blog.csdn.net/dany_zj_cn/article/details/82222975

Spring boot 2.0.4
Thymeleaf 3.0.9
JDK8

@{} 链接地址处理

<a th:href="@{'/thymeleaf'}" >
    跳转到thymeleaf
</a>

th:href 属性中用 @ 前缀输出地址,自动补全ContentPath

某些情况下,项目部署直接部署在 / 根目录下的,如 http://www.abc.com/app/
在模板中使用@{} 自动转换为 href=”/app/thymeleaf”

@前缀 Spring IOC 中Bean的引用

<a th:text="${@abean.size()}" href="#">
    名称为 abean 的 size 方法返回值
</a>

#前缀 特殊对象

${#conversions.convert(val,'String')} 转换服务,调用SpringConversionService
${#fields} org.thymeleaf.spring5.expression.Fields
${#this} 和 ${#root}  代表了表达式上下文的对象,#root就是当前的表达式上下文对象,#this则根据当前求值环境的不同而变化。
${#httpServletRequest}

#{} 多国语言支持/国际化

<a th:text="#{myhref.text}" href="#">
    My
</a>

默认的

# Messages.properties
myhref.text=My

中文的

# Messages-zh.properties
myhref.text=我的

Messages.properties 在 classpath:/ 目录中

[[ ]] 模板页中内嵌的javascript中输出

script 标签需加上 th:inline=”javascript” 才能生效

<script type="text/javascript" th:inline="javascript">
    var href = [[@{/thymeleaf}]];
    var val = [[${}]];
</script>

猜你喜欢

转载自blog.csdn.net/dany_zj_cn/article/details/82222975