Home » Java » java – Make Thymeleaf th:field consume Lombok-generated boolean getter-Exceptionshub

java – Make Thymeleaf th:field consume Lombok-generated boolean getter-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment

Questions:

I’m creating a simple Spring MVC app with Thymeleaf. Being lazy, I’m using Lombok as well. I have a simple DTO, passing to and from Thymeleaf:

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class TypeDto {

    private Long id;
    private String title;
    private boolean isActive;
}

but I’m getting the following error trying to access page:
Bean property 'isActive' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter? It’s failing in the following Thymeleaf snippet:

    <td><input type="checkbox" th:field="*{isActive}"/></td>

If I’d rename isActive to active in both DTO and Thymeleaf template it works fine, so my guess Thymeleaf is trying to read property with getIsActive, which OFC doesn’t exist. As much as I’m up for simple solutions, is there a way to leave boolean as isActive and still make Thymeleaf work?

How to&Answers:

While typing this question, I’ve found out that if I change property matcher in Thymeleaf template itself to active, everything works as expected and no need to change anything on DTO level and below.