Search code examples
thymeleafspring-webflowspring-el

How to access the value of inline map through the value of an enum in SpEL?


I have created a Thymeleaf fragment which I include in all view-states views of my flow.

<th:block th:fragment="flow-messages" 
     th:with="alertTypes = ${ {'INFO':'alert-info','ERROR':'alert-error','WARNING':'alert-warning'} }">
    <div 
       class="alert"
       th:each="message: ${flowRequestContext.messageContext.allMessages}" 
       th:classappend="${alertTypes.get(message.severity)}">
       <p th:text="${message.text}">Message text</p>
    </div>
</th:block>

As you can see above, I initialize a map with severity levels (org.springframework.binding.message.Severity) and the associated bootstrap class.

The problem is that alertTypes.get (message.severity) does not work (returns an empty value). And I have no way to add the corresponding class.

Anyone know how I can fix this?.


Solution

  • Try th:classappend="${alertTypes.get(message.severity.toString())}" instead of th:classappend="${alertTypes.get(message.severity)}".

    The inline map you created has Strings for its keys (and not Enums of type Severity) -- so calling get() with an Enum value won't match.