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>


原文:weiku.co/article/156/
原文地址:https://www.cnblogs.com/xlizi/p/11524128.html