thymeleafspring-thymeleaf

th:style isn't working in my project depending on case true:false


My text have to change color depending on true:false case using Thymeleaf, but whatever I did it doesn't work.

    <table th:each="book : ${assignedBooks}">
        <tr th:style="${book.getExpired()? 'color: red;' : 'color: blue;'}" th:text="${book.getName() + ', ' + book.getAuthor() + ', ' + book.getYear()}">'someBook'</tr>
    </table>

I have checked .getExpired == true, but I don't understand why it still doesn't work.


Solution

  • This is a revised version of your codebase and should work.

    I will explain the changes I carried out.

    <table th:each="book : ${assignedBooks}">
        <tr th:style="${book.expired} ? 'color: red;' : 'color: blue;'"> 
            <td th:text="${book.name + ', ' + book.author + ', ' + book.year}">someBook</td>
        </tr>
    </table>
    

    Additional Tips: