📜 ⬆️ ⬇️

Thymeleaf Tutorial: Chapter 7. Conditional Execution

Table of contents

7 Conditional execution


7.1 Simple conditions: “if” (if) and “unless” (if not)


Sometimes you need a piece of the template to appear only as a result of a certain condition.

For example, imagine that we want to show in the product table a column with the number of comments that exist for each product, and, if there are any comments, a link to the comments page.

To do this, use the th attribute : if :
')
<table> <tr> <th>NAME</th> <th>PRICE</th> <th>IN STOCK</th> <th>COMMENTS</th> </tr> <tr th:each="prod : ${prods}" th:class="${prodStat.odd}? 'odd'"> <td th:text="${prod.name}">Onions</td> <td th:text="${prod.price}">2.41</td> <td th:text="${prod.inStock}? #{true} : #{false}">yes</td> <td> <span th:text="${#lists.size(prod.comments)}">2</span> comment/s <a href="comments.html" th:href="@{/product/comments(prodId=${prod.id})}" th:if="${not #lists.isEmpty(prod.comments)}">view</a> </td> </tr> </table> 

There are a lot of things here, so let's focus on the important line:

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

This code will create a link to the comments page (with the URL / product / comments) with the prodId parameter set to the product ID, but only if the product has any comments.

Let's look at the resulting markup:

 <table> <tr> <th>NAME</th> <th>PRICE</th> <th>IN STOCK</th> <th>COMMENTS</th> </tr> <tr> <td>Fresh Sweet Basil</td> <td>4.99</td> <td>yes</td> <td> <span>0</span> comment/s </td> </tr> <tr class="odd"> <td>Italian Tomato</td> <td>1.25</td> <td>no</td> <td> <span>2</span> comment/s <a href="/gtvg/product/comments?prodId=2">view</a> </td> </tr> <tr> <td>Yellow Bell Pepper</td> <td>2.50</td> <td>yes</td> <td> <span>0</span> comment/s </td> </tr> <tr class="odd"> <td>Old Cheddar</td> <td>18.75</td> <td>yes</td> <td> <span>1</span> comment/s <a href="/gtvg/product/comments?prodId=4">view</a> </td> </tr> </table> 

Fine! This is exactly what we wanted.

Note that the th: if attribute will not only evaluate logical conditions. His capabilities are slightly higher than this, and he will evaluate the specified expression as true, following these rules:

If the value is not null :

If the value is null , th: if will be set to false .

In addition, th: if has an inverse attribute, th: unless, which we could use in the previous example instead of using not (negation) inside an OGNL expression:

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

7.2 Switch statements


There is also a way to conditionally display content using the equivalent of the switch structure in Java: a set of th: switch / switch : case attributes.

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

Note that as soon as one attribute of th: case is evaluated as true, every other attribute of th: case in the same switch context is evaluated as false.

The default parameter is specified as: 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> 

Source: https://habr.com/ru/post/351304/


All Articles