Search code examples
springspring-bootkotlinthymeleaf

Are Thymeleaf model attributes not allowed to start with 'is'?


So I'm writing a springboot application and came across a weird behaviour: If a property name start with is, for example: isIgnoreRequest thymeleaf won't find it but if i change it to ignoreRequest it works.

So my question is: Am I not allowed to have is at the beginning?

Here is some more context:

data class Response(val isIgnoreRequest: Boolean = false,
                    val name: String = StringUtils.EMPTY)

...

//This is how I add the attribute
//Info = Response object
redirectAttributes.addFlashAttribute(ATTRIBUTE_RESPONSE, info)

With the code above thymeleaf can't find the property: Property or field 'isIgnoreRequest' cannot be found on object of type ... - maybe not public or not valid?

If I remove the is it works fine. Even though it sounds stupid I think the is is indeed my problem.


Solution

  • Yes, the model attributes can start with is. The issue isn't coming from thymeleaf, but from kotlin (nice job putting it in the tags). Let me explain:

    When you reference a model attribute in thymeleaf, it looks for the getter/setter method of that attribute using the normal convention; in your example, for the attribute isIgnoreRequest, thymeleaf will look for the methods getIsIgnoreRequest and setIsIgnoreRequest.

    What happens is kotlin generates the getters and setters for isXXX booleans in a different way than the standard, and thymeleaf fails when calling them with the standard syntax. You can see more on how kotlin generates the getters and setters for booleans in

    https://github.com/sockeqwe/fragmentargs/issues/46 or

    https://github.com/sockeqwe/sqlbrite-dao/issues/27

    As to solve your issue, the best solution is probably naming your attributes in a different way so that kotlin doesn't mess with the standard for generating getter and setter methods (which IMO only complicates things unnecessarily; although some frameworks like JSF had a similar issue with isXXX booleans since forever).