0

I am new bee to jsf , I am using prime faces, I did not understand how converter works, in case of single select menu. My confusion is, is it called for converting from request parameter to object in formBean or is it called for rendering my list? In my list If I specify

<f:selectItems
                                value="#{granteeSelectionManager.getGrantProgramDTOs()}"
                                var="grantProgramDTO" itemLabel="#{grantProgramDTO.name}"
                                itemValue="#{grantProgramDTO.id}" />

how to render my list and specify a converter, the converter is beign called for every item in the list?

Please help me understand if it is called for updating selection in my managed bean or for rendering or both ?

user884424
  • 573
  • 1
  • 12
  • 33

1 Answers1

0

It's used for both cases.

When the list is rendered, the converter's getAsString() is used to convert the Java object behind <f:selectItem(s) itemValue> to a String which in turn is rendered as <option value> (which in turn is used as HTTP request parameter). This is indeed done on a per-item basis.

When the form is submitted, the converter's getAsObject() is used to convert the submitted value (the <option value> which appears as HTTP request parameter) back to the concrete Java object so that it can be set in the model (the backing bean) via <x:selectOneMenu value>.


In your particular case you seem to use object's own id property as item value. In such case a converter is completely unnecessary. You only need to make sure that <x:selectOneMenu value> is bound to a property of exactly the same type as <f:selectItem(s) itemValue>, which is probably Integer or Long.

If you however want to get and set a concrete Java object as value like so

<h:selectOneMenu value="#{bean.grantProgramDTO}">
    <f:selectItems ... itemValue="#{grantProgramDTO}" />
</h:selectOneMenu>

then you definitely need a converter for the simple reason that Java objects can't be represented in HTML output and HTTP request parameters without converting them to their unique String representation first. In Java perspective, HTML output is basically one large String and HTTP request parameters are per definition Strings.

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555