0

Say I have the following f:viewParam definition:

<f:metadata>
    <f:viewParam name="cust-id" value="#{CustomerCEVController.customer}" 
        converter="#{customerConverter}" converterMessage="Unknown customer, please use a link from within the system."
        required="true" requiredMessage="cust-id f:viewParam not present"
    />
</f:metadata>

I navigate to the intended page with a "?cust-id=2342" parameter in the URL and so the role of the getAsObject method in the converter is to obviously instantiate the Customer field in the backing bean (e.g. by doing a DB query based on the cust-id value). What's not very clear to me is why we need the getAsString method and how it is employed. This is not a question about the role of the getAsString in converters in the usual case, i.e. in the binding between the .xhtml view UI elements and the backing bean where their role is straightforward. I 've also read here that we can treat the f:viewParam as a UI input element for GET parameters but the role of the converter in the opposite direction doesn't make sense to me.

Community
  • 1
  • 1
Marcus Junius Brutus
  • 23,022
  • 30
  • 155
  • 282

1 Answers1

1

The getAsString() is indeed not used in case of <f:viewParam>, but an existing converter can be reused for other components where getAsString() is really been used.

E.g.

<h:outputText value="#{CustomerCEVController.customer}" converter="#{customerConverter}" />

or

<h:selectOneMenu value="#{CustomerCEVController.customer}" converter="#{customerConverter}">
    <f:selectItems value="#{data.customers}" />
</h:selectOneMenu>

It's up to you whether to implement getAsString() as per its contract or not. If you don't, then your converter would not be reuseable for other components at all.

BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452
  • Strangely using Mojarra 2.1.23 a converter binded to a `` on side load calls 5 times `getAsString`. Saw this behavior when I tried to throw an `UnsupportedOperationException` exception in the mentioned method. – djmj Aug 23 '13 at 00:22