在thymeleaf中解析HTML片段代码

在thymeleaf中一般用th:text来绑定文本数据,但是当出现HTML标签时,如何将代码正确转译呢?

如:

username="<div style='color:red'>Tanya</div>"

使用th:text时

<span th:text="${username}"/>,您好</td>

视图结果:

<div style='color:red'>Tanya</div>,您好

想要的结果:

Tanya,您好

于是需要将th:text改为th:utext

<span th:utext="${username}"/>,您好</td>

视图结果:

Tanya,您好

猜你喜欢

转载自blog.csdn.net/weixin_56322425/article/details/128900734