문제

I have a weird problem with a custom @FacesConverter with JBoss-7.1.0.CR1b during an AJAX call in p:selectOneMenu (Primefaces 3.0).

The simplified Converter looks like this, there are no NPE or other exceptions in this class

@FacesConverter("MyConverter")
public class MyConverter implements Converter
{      
  public Object getAsObject(FacesContext fc, UIComponent uic, String value)
  {
    logger.debug("getAsObject value: "+value);
    if (submittedValue.trim().equals("")) {return null;}
    else
    {           
      MyEjb ejb = new MyEjb();
      ejb.setId(Long.parseLong(value()));
      return ejb;  //**** alternative with return null; ****
    }
  }  
  public String getAsString(FacesContext fc, UIComponent uic, Object value)
  { 
    if (value == null || value.equals("")) {return "";}
    else
    {
        MyEjb ejb = (MyEjb)value;
        return ""+ejb.getId(); 
    }  
  }  
}

The converter is used in a p:selectOneMenu:

<h:form>  
  <p:selectOneMenu value="#{clientBean.selected}" converter="MyConverter">
    <f:selectItems value="#{clientBean.all}" var="my"
                   itemLabel="#{my.name}" itemValue="#{my}"/>
      <p:ajax listener="#{clientBean.changed}" />  
  </p:selectOneMenu>
</h:form>

That's not rocket engineering, the changed method simply makes a debug:

public void changed()
{
  logger.info("changed() "+selected);
}

But now the annoying part: The changed() is never called with the code like above, but I get the converter invoked three times:

12:37:51,500 DEBUG getAsObject value: 35
12:37:51,502 DEBUG getAsObject value:
12:37:51,503 DEBUG getAsObject value:

If I change the p:selectOneMenu value="#{clientBean.selectedId}" to a long selectedId and don't use the Converter the method is called once. Even if I return null in getAsObject()the changed() is called (once). I don't assume it's Primefaces related, because I have the same behavior if I use h:selectOneMenu and f:ajax.

도움이 되었습니까?

해결책

You should have a <p:messages />, <p:growl /> or <h:messages /> which is been ajax-updated in your view. You should also pay attention to warnings in server logs about possible missing faces messages. The chance is big that you're seeing the infamous Validation error: Value not valid validation error.

After conversion, JSF will validate if the submitted object is one of the available items as part of safeguard against tampered/hacked requests. JSF will do that by submittedObject.equals(oneOfAvailableObjects) for each of the available objects as you have there in <f:selectItems>. If nothing matches, then JSF will display this validation error.

In your particular case, the MyEjb class has apparently no equals() method or its implementation is broken. See also Right way to implement equals contract.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top