7.2 switch声明

还有一种有条件地表示内容的方法,与Java里的switch结构等价:th:switch / th:case属性集。

  1. <div th:switch="${user.role}">
  2. <p th:case="'admin'">User is an administrator</p>
  3. <p th:case="#{roles.manager}">User is a manager</p>
  4. </div>

注意:一旦一个th:case属性为true,在同一个switch上下文里的其它的th:case属性会被求值为false

默认选项用th:case="*"指定:

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