Facelet selectOneMenu with POJOs and converter problem

浪子不回头ぞ 提交于 2019-12-06 15:49:54

validation error: value is not a valid selection

This error means that the selected item does not match any of the select items in the list. In your case this can have two causes: either Object#equals() is wrongly implemented, or the getter behind <f:selectItems> doesn't return the same list in the subsequent request (during submitting the form) as it did during the initial request (before submitting the form, during selecting the value).

To exclude the one or other: you can easily test Object#equals() with a "plain vanilla" testcase and you can test the consistency of the list by putting the bean in session scope.

To be clear, your converter looks fine and seems to work fine (else you wouldn't be able to get this kind of error message; the conversion step was thus completed successfully).

I was realy shure that the converter was working correctly, but the final error was in the converter. Maybe this happened someway during hours of debugging. Here is the fixed getAsString method of the converter. N

@Override
public String getAsString(FacesContext context, UIComponent component, Object value) {
    if (value != null && value instanceof Lieferant) {
        Lieferant lieferant = (Lieferant) value;
        return "" + lieferant.getPrimaryKey();
    }
    return "";
}

Now it returns the primaryKey which is used as the value of the selectOneMenu list. Additionally I changed this attribute from the selectItems tag:

 itemValue="#{lieferant}"

Right now I am not shure if this last change was neccessary. But now the lieferants are converted correctly.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!