Search code examples
javathymeleaf

Parameter to String in thymeleaf


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"}.


Solution

  • 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