0

When the parameter *{mydata.value} is enum then how to transform it into String in thymeleaf?

I want to compare

if:*{mydata.value == "aaa"}

It gives error. I think it is because I should do something like:

if:*{mydata.value.toString() == "aaa"}.

Gaurav Jeswani
  • 4,410
  • 6
  • 26
  • 47
wykopowiedz
  • 71
  • 2
  • 12

1 Answers1

1

Try using

if:*{mydata.value.toString().equals("aaa")}

I'm not very familiar with thymeleaf, however that is a common issue in Java. Using '==' will compare the reference to the object, but the .equals() will compare the contents of the string.

If 'mydata.value' is already a string, you can remove the '.toString()'.

If it isn't a string already, you can also use:

if:*{String.valueOf(mydata.value).equals("aaa")}

See more examples of why this happens here

Joshua Burt
  • 76
  • 1
  • 7
  • here https://stackoverflow.com/questions/57906019/how-to-compare-variables-as-string/57906323?noredirect=1#comment102235058_57906323 @Danyal Sandeelo said `for string comparison it's == in thymeleaf` ... so I'm confused now. – wykopowiedz Sep 12 '19 at 17:17
  • @wykopowiedz I'm not really sure honestly. Is your code still not working, even after trying .equals()? – Joshua Burt Sep 12 '19 at 17:28
  • Will try tommorrow, cuz left my computer ;) and I will give you an info – wykopowiedz Sep 12 '19 at 18:01