Spring-Boot Thymeleaf Binding Enum在隐藏的领域

时间:2017-03-30 17:35:11

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

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

我的对象看起来像:

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。我错过了什么?

1 个答案:

答案 0 :(得分:0)

好的,我有问题但在我的问题中无法查看。枚举映射有效,但我课堂上也有:

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

// EQUALS TO SETTER

事实上,我无法相信Getter&amp;塞特被称为,所以这不是一个好主意。我现在将使用带有“普通”Getter&amp ;;的DTO对象。塞特没有任何逻辑。