Thymeleaf 排除标签(不写在标签中)直接显示内容

此文章发布于 60 个月前,部分信息可能已经过时,请自行斟酌确认。

一般情况下 Thymeleaf 模板要输出变量需要在某个标签中(如<div>、<span>)写th:text等属性来实现。但有时我们希望想不写在标签中,直接输出变量的值,比如在 <title> 标签中直接显示变量 msg 的值,而不需要包含在 <span> 等标签中。

解决方案一:

使用 th:block

<title><th:block th:text="${msg}" /> - 服务器错误。</title>

参考:https://www.thymeleaf.org/doc/tutorials/2.1/usingthymeleaf.html#synthetic-thblock-tag

解决方案二(推荐):

使用 inline

<title>[[${msg}]] - 服务器错误。</title>

Hello, [[${user.name}]]!   //[[]]写法会html转义
Hello, [(${user.name})]!   //[()]写法不会html转义

参考:https://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html#inlining

解决方案三:

使用 th:remove

<span th:text="${msg}" th:remove="tag"></span>
最后修改:2019 年 04 月 29 日 10 : 23 AM
如果觉得我的文章对你有用,请随意赞赏

发表评论