selectonemenu验证错误:值无效[重复]

时间:2020-12-30 20:03:14

This question already has an answer here:

这个问题在这里已有答案:

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:

为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 Przedmiot because obj.getClass() returned something like this: Przedmiot_$$_javassist_1. I'm not sure if that is relevant because after all obj proves to be null. What am I doing wrong?

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

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 Przedmiot and 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>

2 个解决方案

#1


4  

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

解决了它。它当然写得很糟糕的是equals()方法。首先,我的问题出了问题。 obj没有解析为null但是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。

#2


1  

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

在您的转换器中:Integer.parseInt(value),在 :selectitems中设置itemvalue>

See also: Why selectOneMenu Send ItemLabel to the converter?

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

#1


4  

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

解决了它。它当然写得很糟糕的是equals()方法。首先,我的问题出了问题。 obj没有解析为null但是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。

#2


1  

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

在您的转换器中:Integer.parseInt(value),在 :selectitems中设置itemvalue>

See also: Why selectOneMenu Send ItemLabel to the converter?

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