0

<p:selectOneMenu value="#{schoolBean.newContactTO.contactType}" converter="contacttypeconverter">
 <f:selectItem itemLabel="select type"></f:selectItem>
 <f:selectItems  value="#{schoolBean.contactTypesConstant.data}" var="contacttype" itemLabel="#{contacttype.type}" itemValue="#{contacttype}"></f:selectItems>
</p:selectOneMenu>
<p:commandButton value="submit" actionListener="#{schoolBean.saveNewContactTO}"></p:commandButton>

I am using custom converter for SelectOneMenu in PrimeFaces. The converter works well for getAsString() but for getAsObject I am not sure what goes wrong. The primefaces UI just gets hanged after this, and henceforth nothing works like I am using dialogs and it doesn't work after that.

Here is my custom converter file:

@FacesConverter("contacttypeconverter")
public class ContactTypeConverter implements Converter {
@Override
public Object getAsObject(FacesContext context, UIComponent component,String type) {
    ContactTypeTO result=null;
        System.out.println("type:"+type);
         if(component instanceof SelectOneMenu){
                    result = new ContactTypeTO();
                    result.setType(""+type);
                    System.out.println("result:"+result);
         }  
        System.out.println(""+result.getType());
    return result;
}
@Override
public String getAsString(FacesContext context, UIComponent component,Object value) {
    String result = "";
    if( value instanceof ContactTypeTO){
        result = ((ContactTypeTO)value).getType();  
    }
    return result;
}}

below is ContactTypeTO

public class ContactTypeTO {

private String type;

  public String getType() {
     return type;
  }

  public void setType(String type) {
     this.type = type;
  }
}

schoolBean.newContactTO.contactType is of type ContactTypeTO (as above)

In getAsObject I am returning a new ContactTypeTO object but I can't figure out why the converter is not able to assign the value to schoolBean.newContactTO.contactType in SelectOneMenu. After I submit, saveNewContactTO doesn't gets execute. The getAsObject method execute as required and also the result variable has proper data while returning. I think there is some issue while converting but I can't figure it out.

  • Thanks @BalusC, it was the same issue you mention. But I am unable to find a solution for it. I am using ((SelectOneMenu)component ).getValue() to get the value and return it. but this doesn't work. Is there any way by which I could get all the selectitems value from the UIComponent argument of getAsObject method? – Sahil Shaikh Sep 09 '15 at 17:17
  • The duplicate contains a link how to create a converter which in turn contains a hint how to create a generic converter based on selectitems. – BalusC Sep 09 '15 at 20:02

0 Answers0