5

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

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Loric
  • 1,678
  • 8
  • 28
  • 48
  • Please add `` or `` and include its ID in `` 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 Jul 19 '13 at 14:38
  • I can add that it's entering the listener method when I select the "none" item in the list. – Loric Jul 19 '13 at 14:38
  • 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 Jul 19 '13 at 14:42
  • This question then duplicates [Validation Error: Value is not valid](http://stackoverflow.com/questions/9069379/validation-error-value-is-not-valid) – BalusC Jul 19 '13 at 14:45

1 Answers1

8

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;
    }
}
Loric
  • 1,678
  • 8
  • 28
  • 48