【问题标题】:Make Thymeleaf th:field consume Lombok-generated boolean getter让 Thymeleaf th:field 使用 Lombok 生成的布尔 getter
【发布时间】:2020-02-18 13:43:19
【问题描述】:

我正在使用 Thymeleaf 创建一个简单的 Spring MVC 应用程序。懒惰,我也在使用 Lombok。我有一个简单的 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 sn-p 中失败:

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

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

【问题讨论】:

    标签: java spring thymeleaf lombok


    【解决方案1】:

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

    【讨论】:

    • 您在发布表单时不会遇到问题吗?
    • 没有。只要我只有一个属性匹配active,它就像一个魅力,
    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 2017-07-25
      • 1970-01-01
      • 2017-06-24
      • 1970-01-01
      • 2012-08-01
      • 2020-05-19
      • 1970-01-01
      • 1970-01-01
      • 2015-03-07
      相关资源
      最近更新 更多