5
votes

Here is my SelectOneMenu

<h:selectOneMenu value="#{bean.myObject}" >
    <f:ajax render="componentToRender" listener="#{bean.onSelect}"/>
    <f:converter converterId="myObjectConverter" />
    <f:selectItem itemLabel="None" itemValue="#{null}" />
    <f:selectItems value="#{bean.objects}" var="object" itemValue="#{object}" itemLabel="#{object.name}" />
</h:selectOneMenu>

And my converter

@FacesConverter("myObjectConverter")
public class MyObjectConverter implements Converter{

    private List<MyObject> objects;

    public MyObjectConverter(){
        this.objects = MyController.getAllMyObjects();
    }

    public Object getAsObject(FacesContext context, UIComponent component, String value) {
        if(!StringUtils.isInteger(value)) {
            return null;
        }
        return this.getMyObject(value);
    }

    public String getAsString(FacesContext context, UIComponent component, Object value) {
        if(value == null) {
            return null;
        }
        return String.valueOf(((MyObject) value).getId()).toString();
    }

    public MyObject getMyObject(String id) {
        Iterator<MyObject > iterator = this.objects.iterator();
        while(iterator.hasNext()) {
            MyObject object = iterator.next();

            if(object.getId() == Integer.valueOf(id).intValue()) {
                return object;
            }
        }
        return null;
    }

}

The problem is that my ajax listener is never called and my component never rendered. Is there something wrong with my converter or selectOneMenu? I follow an example and I can't figure the mistake out.

BTW : my simple method in the bean

public void onSelect() {
    System.out.println(this.myObject);
    if(this.myObject != null) {
        System.out.println(this.myObject.getName());
    }
}

I already had a problem like this and I changed my selected value from object to id. But here I want to make it work with objects because I know it's possible.

Thanks

1
Please add <h:message> or <h:messages> and include its ID in <f:ajax render> as well. Chance is big that you'll finally get the desired conversion error faces message. Or, just look in server logs for enqueued but undisplayed faces messages. Or perhaps there's even a concrete exception in there which should already be the whole answer at its own, such as NullPointerException which indicates a rather trivial Java logic problem. - BalusC
I can add that it's entering the listener method when I select the "none" item in the list. - Loric-
Yes, you're right, I have a validation error (the value is incorrect). That mean it's my converter? Its a very simple converter I thought. - Loric-
This question then duplicates Validation Error: Value is not valid - BalusC

1 Answers

8
votes

I have the solution. I had to override the "equals" method in MyObject class!

Thanks.

EDIT: the code

@Override
public boolean equals(Object obj) {
    if(this.id == ((MyObject) obj).id) {
        return true;
    }else {
        return false;
    }
}