I've been working with converters in my PrimeFaces SelectOneMenu objects. They work fine if I only tell which class the converter is referring to:
@FacesConverter(forClass = DescriptorVO.class)
public class DescriptorVOConverter implements Converter { ... }
This way, I don't have to explicitly tell the JSF component which converter should be used when it is populated with objects of class DescriptorVO
.
However, I made a page that used a p:SelectManyCheckbox
and I couldn't for the life of me know why it wasn't calling my converter. Then I gave it a name, like so:
@FacesConverter(forClass = RoleVO.class, value = "roleVOConverter")
public class RoleVOConverter implements Converter { ... }
and passed it as one of the component's properties
<p:selectManyCheckbox id="cbx_roles" required="true" converter="roleVOConverter"
requiredMessage="At least one role must be selected."
value="#{userView.selectedRoles}" layout="responsive">
<f:selectItems value="#{userView.roles}" var="role"
itemLabel="#{role.title}" itemValue="#{role}" />
</p:selectManyCheckbox>
and voi la, it started calling the converter correctly. This raised a question to me regarding when I should name my converters (through the value
attribute) and when telling them which class the converter should be used with (with the forClass
attribute) is enough. I never had to namy any converters when working with PrimeFaces, only for this particular SelectManyCheckbox
component. Do different components have different necessities regarding converters or did I just get the concept of converters wrong?