【问题标题】:Spring-Boot Thymeleaf Binding Enum at hidden fieldSpring-Boot Thymeleaf 绑定枚举在隐藏字段
【发布时间】:2017-08-24 18:49:11
【问题描述】:

我想在一个隐藏字段绑定一个枚举,以便在提交表单时保留信息。不知何故,枚举的字段为空。

我的对象看起来像:

public class Check {
    @NotNull        private String name;
    @NotNull        private CheckType type; // ENUM with GETTER & SETTER

    // BESIDE NORMAL GETTER & SETTER ANOTHER TRY ...
    public String getTypeName() {
        return type == null ? null : type.name();
    }

    public void setTypeName(String name) {
        type = CheckType.valueOf(name);
    }

    ...
}

在我的 Thymeleaf-Template 中,我正在遍历对象并希望保留枚举:

<input type="hidden" th:field="*{checks[__${checkStat.index}__].type}" />       

<!-- ALSO TRIED ... -->
<input type="hidden" th:field="*{checks[__${checkStat.index}__].typeName}" />

发送Form后,type为null。我错过了什么?

【问题讨论】:

    标签: forms spring-mvc spring-boot enums thymeleaf


    【解决方案1】:

    好的,我有问题,但在我的问题中看不到。枚举映射有效,但我在课堂上也有类似:

    public Integer getSpecialValue() {
        if (type == SOMETHING) {
            return rareEventThreshold;
        } else {
            throw new IllegalStateException();
        }
    }
    
    // EQUALS TO SETTER
    

    事实上,我无法相信调用 Getter 和 Setter 的顺序,所以这不是一个好主意。我现在将使用带有“普通”Getter 和 Setter 的 DTO 对象,没有任何逻辑。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-10-01
      • 2018-10-09
      • 2021-01-23
      • 1970-01-01
      • 2015-11-16
      • 1970-01-01
      • 2012-01-16
      • 2014-01-26
      相关资源
      最近更新 更多