What is the purpose of the th:if
attribute in Thymeleaf?
A) To conditionally include content based on a boolean expression
B) To iterate over a collection
C) To format dates
D) To include an external template
Answer:
A) To conditionally include content based on a boolean expression
Explanation:
The th:if
attribute in Thymeleaf is used to conditionally include or exclude parts of the template based on a boolean expression. If the expression evaluates to true, the content is included; otherwise, it is excluded.
For example:
<p th:if="${user != null}">Welcome, [[${user.name}]]!</p>
In this example, the paragraph element is only included if the user
object is not null.