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

  • Post author:
  • Post category:其他


6.1 th:text属性

可对表达式或变量求值,并将结果显示在其被包含的 html 标签体内替换原有html文本
文本连接:用“+”符号,若是变量表达式也可以用“|”符号
6.2 th:utext属性
e.g.

若home.welcome=Welcome to our <b>fantastic</b> grocery store!

用<p th:text=”#{home.welcome}”></p>解析结果为:

<p>Welcome to our <b>fantastic</b> grocery store!</p>

解决方案

<p th:utext=”#{home.welcome}”></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="${user.isAdmin} == false"> ...</span></li>
				<li>获取对象的方法(public):<span th:if="${user.isAdmin()} == false"> ...</span></li>
			</ol>
		</li>
		<li>null:<span th:if="${user.isAdmin()} == null"> ...</span></li>
		<li>连接:
			<ol>
				<li>连接符+:<span th:text="'The name of the user is ' + ${user.userName}"></span></li>
				<li>连接符|:<span th:text="|Welcome to our application, ${user.userName}!|"></span></li>
				<li><span th:text="'Welcome to our application, ' + ${user.userName} + '!'"></span></li>
				<li><span th:text="${onevar} + ' ' + |${twovar}, ${threevar}|"></span></li>
				<li><span th:utext="#{home.welcome}">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>



版权声明:本文为sun_jy2011原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。