<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.