Thymeleaf - How to Apply Two (Or More) Styles Based on Mutually Exclusive Conditions

Thymeleaf - How to apply two (or more) styles based on mutually exclusive conditions

Thymeleaf has a th:styleappend attribute that allows for multiple styles to be applied:

<tr th:each="item, rowStat : ${items}" 
th:style="${rowStat.odd} ? 'background: #f0f0f2;' : 'background: #ffffff;'"
th:styleappend="${item.getValue()} > 5 ? 'color: red;' : 'color: black;'">

<td.... <!-- cols>

</tr>

Thymeleaf add value in style

Most probably you are having a syntax issue here. Try the following code:

th:styleappend="'background-position:-' + @{${value}} + '%'"

Thymeleaf multiple conditions, change background color

Consider using the variable as class to simplify the Thymeleaf and HTML. Then you can set the format consistently with your CSS. Your HTML would look something like this:

<tbody>
<tr th:each = "accountDTO : ${listOfAccounts}">
<td th:text = "${accountDTO.accountDTO}"></td>
<td th:text = "${accountDTO.startBalance}"></td>
<td th:text = "${accountDTO.currentBalance}"></td>
<td th:text = "${accountDTO.state}" th:classappend = "${accountDTO.state}"></td>
<td th:text = "${accountDTO.employee.employeeName}"></td>
<td><a th:href = "@{/editAccount/{id}(id=${accountDTO.idAccount})}" class="btn btn-primary btn-lg" id = "eBtn" th:data = "${accountDTO.idAccount}">Edit</a></td>
<tr>
</tbody>

Then look at something like this in your CSS:

td.OUT_OF_USE {
background: red;
}
td.DEVICE {
background: green;
}
td.LOCKED {
background: blue;
}

Possibly useful for making similar output consistent on seperate pages.

How can I use multiple conditions (and, or) in thymeleaf th:if

You can combine them

th:if="${lang == 'A' OR lang == 'B'}"

Difference between Spring MVC and Spring Boot

  • Spring MVC is a complete HTTP oriented MVC framework managed by the Spring Framework and based in Servlets. It would be equivalent to JSF in the JavaEE stack. The most popular
    elements in it are classes annotated with @Controller,
    where you implement methods you can access using different HTTP requests. It has an equivalent @RestController to implement REST-based APIs.
  • Spring boot is a utility for setting up applications quickly, offering an out of the box configuration in order to build
    Spring-powered applications. As you may know, Spring integrates
    a wide range of different modules under its umbrella, as spring-core, spring-data,
    spring-web (which includes Spring MVC, by the way) and so on. With this tool you can tell Spring how many of them to use and you'll get a fast setup for them (you are allowed to change it by yourself later on).

So, Spring MVC is a framework to be used in web applications and Spring Boot is a Spring based production-ready project initializer. You might find useful visiting the Spring MVC tag wiki as well as the Spring Boot tag wiki in SO.



Related Topics



Leave a reply



Submit