使 Thymeleaf th:field 消耗 Lombok 生成的布尔值 getter

Make Thymeleaf th:field consume Lombok-generated boolean getter

我正在使用 Thymeleaf 创建一个简单的 Spring MVC 应用程序。懒惰,我也在使用龙目岛。我有一个简单的 DTO,传入和传出 Thymeleaf:

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

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

但我在尝试访问页面时遇到以下错误: 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? 以下 Thymeleaf 片段失败了:

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

如果我在 DTO 和 Thymeleaf 模板中将 isActive 重命名为 active 它工作正常,所以我猜 Thymeleaf 正在尝试用 getIsActive 读取 属性 ,哪个 OFC 不存在。尽管我想要简单的解决方案,但有没有办法将布尔值保留为 isActive 并仍然使 Thymeleaf 工作?

在输入这个问题时,我发现如果我将 Thymeleaf 模板本身中的 属性 匹配器更改为 active,一切都会按预期进行,并且无需在 DTO 级别上进行任何更改,并且下面。

从字段名称中删除 'is'。

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