[英]Checkbox in JHipster
I have a checkbox in the html我在 html 中有一个复选框
<input type="checkbox" class="form-control" name="myflag" [(ngModel)]="myEntity.myflag" id="field_myflag">
The field (myflag) on DB has 2 possible value [OK, KO] and these 2 values are defined in an enumeration: DB 上的字段 (myflag) 有 2 个可能的值 [OK, KO],这两个值在枚举中定义:
@Enumerated(EnumType.STRING)
@Column(name = "MYFLAG")
private MYFLAGENUM myflag;
so:所以:
public enum MYFLAGENUM {
OK, KO
}
Every time I try to save, I reach an exception:每次尝试保存时,都会遇到异常:
WARN 888 --- [ XNIO-7 task-5] .mmaExceptionHandlerExceptionResolver : Resolved exception caused by handler execution: org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize instance of
myproject.hipster.testing.domain.enumeration.MYFLAGENUM
out of VALUE_FALSE token;警告 888 --- [XNIO-7 task-5] .mmaExceptionHandlerExceptionResolver:由处理程序执行引起的已解决异常:org.springframework.http.converter.HttpMessageNotReadableException:JSON 解析错误:无法反序列化myproject.hipster.testing.domain.enumeration.MYFLAGENUM
实例myproject.hipster.testing.domain.enumeration.MYFLAGENUM
超出 VALUE_FALSE 令牌; nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance ofmyproject.hipster.testing.domain.enumeration.MYFLAGENUM
out of VALUE_FALSE token at [Source: (PushbackInputStream);嵌套异常是 com.fasterxml.jackson.databind.exc.MismatchedInputException:无法从 VALUE_FALSE 令牌中反序列化myproject.hipster.testing.domain.enumeration.MYFLAGENUM
实例 [Source: (PushbackInputStream); line: 1, column: 37] (through reference chain: myproject.hipster.testing.domain.MYENTITY["myflag"])行:1,列:37](通过参考链:myproject.hipster.testing.domain.MYENTITY["myflag"])
It seems VALUE_FALSE (or VALUE_TRUE) could not be converted to 'KO' (or 'OK').似乎 VALUE_FALSE(或 VALUE_TRUE)无法转换为“KO”(或“OK”)。 So I create a converter, but I see it is not called.所以我创建了一个转换器,但我看到它没有被调用。 How can I manage my own "boolean" value with JHipster checkbox?如何使用 JHipster 复选框管理我自己的“布尔值”值?
Try尝试
...
@JsonDeserialize(using = OkKoDeserializer.class)
private MYFLAGENUM myflag;
and:和:
public class OkKoDeserializer extends StdScalarDeserializer<MYFLAGENUM> {
public OkKoDeserializer() {
super(MYFLAGENUM.class);
}
@Override
public MYFLAGENUM deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
String value = p.getValueAsString();
if ("VALUE_FALSE".equalsIgnoreCase(value)) {
return MYFLAGENUM.KO;
}
if ("VALUE_TRUE".equalsIgnoreCase(value)) {
return MYFLAGENUM.OK;
}
throw new IllegalArgumentException("value " + value + " is not parseable to a MYFLAGENUM");
}
}
This way you tell the framework to use a special strategy to convert the input value to the enum value.通过这种方式,您告诉框架使用特殊策略将输入值转换为枚举值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.