Thymeleaf 在页面中直接显示内容


Thymeleaf 在页面中直接显示内容

 

一般情况下 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>


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM