How to use thymeleaf conditions - if - elseif - else
You conditional operator contains 3 results. It should have 2 like this.
condition ? first_expression : second_expression;
In your situation. I assume linea.estado
is a boolean
value
<td style="white-space: nowrap">
<span th:class="${linea.estado} ? 'label label-success' : 'label label-danger'"
th:text="${linea.estado}? #{label.glineas.estado.iniciado} : #{label.glineas.estado.finalizado}">
</span>
</td>
If you want 3 values to be output and given that the linea.estado
is a string which may contain 'WARN', 'DANGER', 'INFO'
then you can do something like this.
<span th:class="'label label-' + ((${linea.estado} == 'SUCCESS') ? 'success' : (${linea.estado} == 'DANGER') ? 'danger' : 'warning')"
th:text="...">
</span>
But the cleaner solution will be something like this
<span th:if="${linea.estado} == 'SUCCESS'" class="label label-success" th:text="#{label.glineas.estado.iniciado}"></span>
<span th:if="${linea.estado} == 'DANGER'" class="label label-danger" th:text="#{label.glineas.estado.finalizado}"></span>
<span th:if="${linea.estado} == 'WARN'" class="label label-warning" th:text="#{label.glineas.estado.configurado}"></span>
Or using Switch
as mentioned by Patrick LC
- be aware of syntax errors, as I didnt test any codes on runtime
I think the solution is to use the switch statement, from Thymeleaf documentation:
<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>
There isn't any other structure to use in Thymeleaf, although you could use th:if/th:unless. Check this thread in the thymeleaf forum.