using thymeleaf之六th:text/th:utext属性

时间:2025-02-15 08:05:39

6.1 th:text属性

可对表达式或变量求值,并将结果显示在其被包含的 html 标签体内替换原有html文本
文本连接:用“+”符号,若是变量表达式也可以用“|”符号
6.2 th:utext属性
.
若=Welcome to our <b>fantastic</b> grocery store!
用<p th:text="#{}"></p>解析结果为:
<p>Welcome to our <b>fantastic</b> grocery store!</p>
解决方案
<p th:utext="#{}"></p>即可。
等效于<p>Welcome to our <b>fantastic</b> grocery store!</p>

如:
<div>
		<ol>
		<li>文本:
			<ol>
				<li>'xxxx':Now you are looking at a <span th:text="'working web application'">template file</span>.</li>
				<li>不能有空格、逗号:Now you are looking at a <span th:text="workingwebapplication">template file</span>.</li>
			</ol>
		</li>
		<li>数字:
			<ol>
				<li>The year is <span th:text="2014">1492</span>.</li>
				<li>In two years, it will be <span th:text="2014 + 2">1494</span>.</li>
			</ol>
		</li>
		<li>布尔值(true/false):
			<ol>
				<li>获取对象属性:<span th:if="${} == false"> ...</span></li>
				<li>获取对象的方法(public):<span th:if="${()} == false"> ...</span></li>
			</ol>
		</li>
		<li>null:<span th:if="${()} == null"> ...</span></li>
		<li>连接:
			<ol>
				<li>连接符+:<span th:text="'The name of the user is ' + ${}"></span></li>
				<li>连接符|:<span th:text="|Welcome to our application, ${}!|"></span></li>
				<li><span th:text="'Welcome to our application, ' + ${} + '!'"></span></li>
				<li><span th:text="${onevar} + ' ' + |${twovar}, ${threevar}|"></span></li>
				<li><span th:utext="#{}">Welcome to our grocery store!</span></li>
			</ol>
		</li>
		<li>th:utext:
			<ol>
				<li>th:text:<span th:text="${textWelcome}">textWelcome!</span></li>
				<li>th:utext:<span th:utext="${textWelcome}">textWelcome!</span></li>
			</ol>
		</li>
		</ol>
	</div>