般情況下 Thymeleaf 模板要輸出變量需要在某個(gè)標(biāo)簽中(如<div>、<span>
)寫th:text
等屬性來實(shí)現(xiàn)。但有時(shí)我們希望想不寫在標(biāo)簽中,直接輸出變量的值,比如在 <title>
標(biāo)簽中直接顯示變量 msg
的值,而不需要包含在 <span>
等標(biāo)簽中。
解決方案一:
使用 th:block
<title><th:block th:text="${msg}" /> - 服務(wù)器錯(cuò)誤。</title>
解決方案二(推薦):
使用 inline
<title>[[${msg}]] - 服務(wù)器錯(cuò)誤。</title> Hello, [[${user.name}]]! //[[]]寫法會(huì)html轉(zhuǎn)義 Hello, [(${user.name})]! //[()]寫法不會(huì)html轉(zhuǎn)義
解決方案三:
使用 th:remove
<span th:text="${msg}" th:remove="tag"></span>