Search code examples
cssspringspring-bootspring-mvcthymeleaf

Thymeleaf Could not parse as expression when trying to change background color


I am trying to change the background of a div depending of a variable systemStatus from an object taken from a database.

th:style="${system.systemStatus} == 1} ? 'background: green' : 'background: red'"

But I get the error:

org.thymeleaf.exceptions.TemplateProcessingException: Could not parse as expression: "${system.systemStatus} == 1} ? 'background: green' : 'background: red'" (template: "system-management" - line 48, col 41)

I've tried to make a function to get systemStatus as a string:

th:style="${system.systemStatusString()} == '1'} ? 'background: green' : 'background: red'"

but I get the same error.

The whole div:

<div class="form-group" th:style="${system.systemStatus} == 1} ? 'background: green' : 'background: red'">
    <label for="id">ID: </label>
    <input type="text" th:value="${system.id}" th:name="id" th:id="id"  class="form-control" id="id" readonly />
    <br>
    <label for="machineId">Machine ID: </label>
    <input type="text" th:value="${system.machineId}" th:name="machineId" th:id="machineId"  class="form-control" id="machineId"/>
</div>

Solution

  • You have an extra }. The actual code should be like

    th:style="${system.systemStatus} == 1 ? 'background: green' : 'background: red'"