jsf 2 - When to name a converter in JSF -
i've been working converters in primefaces selectonemenu objects. work fine if tell class converter referring to:
@facesconverter(forclass = descriptorvo.class) public class descriptorvoconverter implements converter { ... }
this way, don't have explicitly tell jsf component converter should used when populated objects of class descriptorvo
.
however, made page used p:selectmanycheckbox
, couldn't life of me know why wasn't calling converter. gave name, so:
@facesconverter(forclass = rolevo.class, value = "rolevoconverter") public class rolevoconverter implements converter { ... }
and passed 1 of component's properties
<p:selectmanycheckbox id="cbx_roles" required="true" converter="rolevoconverter" requiredmessage="at least 1 role must selected." value="#{userview.selectedroles}" layout="responsive"> <f:selectitems value="#{userview.roles}" var="role" itemlabel="#{role.title}" itemvalue="#{role}" /> </p:selectmanycheckbox>
and voi la, started calling converter correctly. raised question me regarding when should name converters (through value
attribute) , when telling them class converter should used (with forclass
attribute) enough. never had namy converters when working primefaces, particular selectmanycheckbox
component. different components have different necessities regarding converters or did concept of converters wrong?
that can happen when value
of uiselectmany
component references generic java.util.collection
type list<role>
instead of array role[]
. specified in javadoc of uiselectmany
(emphasis mine):
obtain
converter
using following algorithm:
- if component has attached
converter
, use it.if not,
valueexpression
value (if any).valueexpression
must point is:
- an array of primitives (such
int[]
). registered by-classconverter
primitive type.- an array of objects (such
integer[]
orstring[]
). registered by-classconverter
underlying element type.- a
java.util.collection
. not convert values.if reason
converter
cannot found, assume typestring
array.
the reason is, generic type <role>
lost during runtime , not directly determinable. works when you're using myfaces instead of mojarra in case of java.util.collection
inspect actual type manually iterating on <f:selectitem(s)>
, determine converter
based on first non-null
value.
i created spec issue 1422 on in jsf spec , improve mojarra too.
Comments
Post a Comment