#th:if

th:if属性求Bool值,只有true的时候其所在的标签及该标签中的内容才会被渲染到输出结果中

<a href="comments.html"
   th:href="@{/product/comments(prodId=${prod.id})}" 
   th:if="${not #lists.isEmpty(prod.comments)}">view</a>

th:if=“expression”expression求值有下述规则

  • 值不为null,下列表达式都求值为true
  • booleantrue本身
  • 0
  • '0'字符
  • "false", "off", "no"之外的字符串
  • boolean,number,character,string之外的任意其他对象
  • 值为null被认为是false

#th:unless

th:unless是th:if的一个相反操作,上面的例子可以改写为

<a href="comments.html"
   th:href="@{/comments(prodId=${prod.id})}" 
   th:unless="${#lists.isEmpty(prod.comments)}">view</a>

#th:switch/th:case

<div th:switch="${user.role}">
  <p th:case="'admin'">User is an administrator</p>
  <p th:case="#{roles.manager}">User is a manager</p>
  <p th:case="*">User is some other thing</p>
</div>

一旦某个case求值为true,剩余的case则都当做false“*”指明默认case

Logo

一站式虚拟内容创作平台,激发创意,赋能创作,进入R空间,遇见同道,让优质作品闪耀发光。​

更多推荐