java selectonemenu 验证错误:值无效

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16050051/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 21:43:59  来源:igfitidea点击:

selectonemenu Validation Error: Value is not valid

javahibernatejsf

提问by user2270884

I'm using selectonemenu like this:

我正在使用 selectonemenu 这样的:

<h:selectOneMenu value="#{MyBean.zajecie.przedmiot}">
    <f:selectItems value="#{MyBean.przedmioty}" var="p"
        itemLabel="#{p.nazwa}" itemValue="#{p}" />
    <f:converter converterId="converter.PrzedmiotConverter" />
</h:selectOneMenu>

MyBean:

我的豆:

private Zajecie zajecie;//+set get
private List<Przedmiot> przedmioty;//+set get

@PostConstruct
private void init() {
    przedmioty = przedmiotDao.findByLogin("login");
    zajecie = new Zajecie();
}

and the converter methods:

和转换器方法:

public Object getAsObject(FacesContext context, UIComponent component, String value) {
    PrzedmiotDao przedmiotDao = DaoFactory.getInstance().getPrzedmiotDao();
    Przedmiot przedmiot = przedmiotDao.findById(Przedmiot.class, Integer.parseInt(value));
    return przedmiot;
}

public String getAsString(FacesContext context, UIComponent component, Object value) {
    Przedmiot przedmiot = (Przedmiot) value;
    String idAsString = String.valueOf(przedmiot.getPrzedmiotId());
    return idAsString;
}

The selectonemenu component is being populated as it's supposed to. When I submit, it shows Validation Error: Value is not valid. I know i need a proper equals()method for my entities so I've generated it with eclipse using only the id field. Then i had to change the test getClass() != obj.getClass()to obj instanceof Przedmiotbecause obj.getClass()returned something like this: Przedmiot_$$_javassist_1. I'm not sure if that is relevant because after all objproves to be null. What am I doing wrong?

selectonemenu 组件正在按预期填充。当我提交时,它显示Validation Error: Value is not valid. 我知道equals()我的实体需要一个合适的方法,所以我只使用 id 字段用 eclipse 生成了它。然后我不得不测试更改getClass() != obj.getClass()obj instanceof Przedmiot因为obj.getClass()返回类似这样的:Przedmiot_$$_javassist_1。我不确定这是否相关,因为毕竟obj证明是null. 我究竟做错了什么?

Edit:

编辑:

MyBean is ViewScoped.

MyBean 是 ViewScoped。

Funny thing is that similar code using the same converter works in an other part of the application. The difference is that in the working part I'm just viewing the list of type Przedmiotand I'm obtaining it in another way.

有趣的是,使用相同转换器的类似代码可以在应用程序的其他部分运行。不同之处在于,在工作部分,我只是查看类型列表,Przedmiot而我正在以另一种方式获取它。

@PostConstruct
private void init() {
    student = studentDao.findByLogin(ra.getUser());
}

<h:selectOneMenu value="#{otherBean.przedmiot}">
    <f:selectItems value="#{otherBean.student.grupa.przedmiots}" var="p" 
        itemLabel="#{p.nazwa}" itemValue="#{p}" />
    <f:converter converterId="converter.PrzedmiotConverter" />
</h:selectOneMenu>

回答by user2270884

Solved it. It was of course badly written equals()method. First of all there was a mistake in my question. objdidn't resolve to null but other.przedmiotIddid. Sorry for that. Look at the method generated by eclipse:

解决了。这当然是写得很糟糕的equals()方法。首先,我的问题有一个错误。obj没有解决为空,但other.przedmiotId做到了。对不起。看eclipse生成的方法:

@Override
public boolean equals(Object obj) {
    if (this == obj)
        return true;
    if (obj == null)
        return false;
    if (!(obj instanceof Przedmiot))//changed this from (getClass() != obj.getClass())
        return false;
    Przedmiot other = (Przedmiot) obj;
    if (przedmiotId == null) {
        if (other.przedmiotId != null)
            return false;
    } else if (!przedmiotId.equals(other.przedmiotId))
        return false;
    return true;
}

The issue is in other.przedmiotId. When obtaining the value with a getter other.getPrzedmiotId()it doesn't resolve to null anymore.

问题在other.przedmiotId. 使用 getter 获取值时,other.getPrzedmiotId()它不再解析为 null。

回答by Rong Nguyen

In your converter: Integer.parseInt(value), and in <f:selectItemsyou set itemValue="#{p}", so each #{p}is instance of Przedmiottype.

在您的 converter:Integer.parseInt(value)<f:selectItems您的 set 中itemValue="#{p}",因此每个#{p}都是Przedmiot类型的实例。

See also: Why selectOneMenu Send ItemLabel to the converter?

另请参阅: 为什么 selectOneMenu 将 ItemLabel 发送到转换器?