The env: Mojarra 2.2.12 Primefaces 5.3 Glassfish 4.1.1
JSF page
........
<p:selectOneMenu id="system" value="#{permissionController.permission.system}"
converter="entityConverter"
>
<p:ajax event="change"
listener="#{permissionController.buildObjectTypeList}"
update="permission_details_panel" />
<f:selectItem itemLabel="#{msg['please.select']}" itemValue="#{null}" />
<f:selectItems value="#{permissionController.systems}" var="_system" itemValue="#{_system}"
itemLabel="#{_system.name}"/>
</p:selectOneMenu>
..........
</h:panelGroup>
Converver class
@FacesConverter(value = "entityConverter")
public class EntityConverter extends SelectItemsConverter {
@Override
public Object getAsObject(FacesContext context, UIComponent component, String value) {
Object o = super.getAsObject(context, component, value);
if (null == o) {
return null;
}
return o;
}
@Override
public String getAsString(FacesContext context, UIComponent component, Object value) {
if (null == value || !(value instanceof IEntity)) {
return null;
}
IEntity entity = (IEntity) value;
if (null == entity.getId()) {
return null;
}
return entity.getId().toString();
}
}
Scenario
- Select a system in the selectOneMenu component with id="system"- a buildObjectTypeList method is called on the backing bean
- Select a default "Select One..." item which has null as a value on the same component - the buildObjectTypeList method is NOT called on the backing bean
That happens because when an item with null value is selected, the value from itemLabel comes to the converter's getAsString
instead of the null. So when it happens the 'value' parameter in the public String getAsString(FacesContext context, UIComponent component, Object value)
will have "Select One..." as a value and later based on this value a current item will be select on the JSF page. Obviously there no such item, because the item's value is null
This issue was already raised not even once, but was never addressed. Below are the links. I used a workaround, but it is heavy and looks too synthetic for this issue. It would be nice if the the actual value from f:selectItem
was passed to the coverter instead of a value from itemLabel attribute, but I have no idea how I can make it to work this way.
Does it look like a Primefaces framework problem?
primefaces selectonemenu change event not working for null value