简体   繁体   中英

Make Thymeleaf th:field consume Lombok-generated boolean getter

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?

在输入这个问题时,我发现如果我将 Thymeleaf 模板本身中的属性匹配器更改为active ,一切都按预期工作,无需在 DTO 级别及以下更改任何内容。

从字段名称中删除“is”。

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM