How to check for that map is containg the particular key using thymleaf

StackOverflow https://stackoverflow.com/questions/22559367

  •  18-06-2023
  •  | 
  •  

Pregunta

I am using the thymeleaf for presentation layer, so from controller i am sending the map containing key and list of values like this Map<Long,List<Long>> to x.html. But how i check whether the key contained in map in x.html using thymeleaf so please tell me the way to check it

i tried this way but not successfull

<span th:if="${#maps.containsKey(myMap, myStringValue)}">YEAH!</span>
¿Fue útil?

Solución

There is such method you described since Thymeleaf version 1.0 which works for me as expected (see documentation). Maybe your Map key isn't String value or myStringValue isn't String.

Did you tried use constant String as key?

<span th:if="${#maps.containsKey(myMap, 'valueOfMyStringValue')}">YEAH!</span>

Or call Map#containsKey method directly on Map?

<span th:if="${myMap.containsKey('valueOfMyStringValue')}">YEAH!</span>
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top