2 Replies Latest reply: Apr 18, 2012 8:53 AM by Andrey Kotov RSS

JSF Question about passing parameters (needing converters) to resource bundles from facelets...

Chris Davis Newbie

have a JSF question about how to pass mutliple complex parameters to resource bundles. Take the following example below where I am passing a value to a resource bundle for a parameter. Works fine.

 

<h:outputFormat value="#{messages['label.widget.count']}">
<f:param value="#{widgetCount}"/>
</h:outputFormat>

 

Now take the example(s) below using a converter or more complicated piece of data that needs something done to it and am passing a value to a resource bundle for a parameter. This doesn’t work (it returns a null value even though outside of it it runs fine) . I’ve tried everything and can’t figure out how to do it. Any ideas?

 

<h:outputFormat value="#{messages['label.welcomemessagewithdate']}">
<f:param value="#{widget.date}">
<f:converter converterId=”dateConverter” />
</f:param>
</h:outputFormat>
OR
<h:outputFormat value="#{messages['label.welcomemessagewithdate']}">
<f:param>
<h:outputText value="#{widget.date}">
<f:converter converterId=”dateConverter” />
</h:outputText>
</f:param>
</h:outputFormat>

 

So the basic question is how can I pass a parameter to a resource bundle in the form of “widge.message=Hello {0}, the time is {1}” where 1 needs a converter with it to format it correctly?

 

Thanks in advance!

 

Chris

  • 1. Re: JSF Question about passing parameters (needing converters) to resource bundles from facelets...
    Stan Silvert Master

    Hi Chris,

     

    I took a close look at this using Mojarra 2.0.  When I put a <f:converter> tag inside a <f:param> tag and I got this exception:

    javax.faces.view.facelets.TagException: /index.xhtml @23,60 <f:converter> Parent not an instance of ValueHolder: javax.faces.component.UIParameter@1675bf3

    According to javadoc, <f:param> doesn't support converters.  However, if used with some tags, such as <h:link>, you can use the converterId attribute to pass a converter.  This works:

    <h:link value="Hello Link">
      <f:param name="foo" value="#{myDate}" converterId="javax.faces.DateTime"/>
    </h:link>

     

    The thing about <f:param> is that the parent component gets to decide how to treat it.

    Anyway, I haven't looked at the code for the tag, but it looks like <h:outputFormat> won't look for converters on its <f:param> children. 

    I think the easiest workaround for you is to use a managed bean to do the conversion.  You could just let #{widget} return the date as a string that is formatted the way you want it.
    <f:param value="#{widget.convertedDate}">

    If you were using JBoss AS6, you could take advantage of EL ValueExpressions with params and do something like this:

    <f:param value="#{myConverterBean(widget.date)}">

     

    Another approach is to use some logic that changes the value in the component tree during the JSF lifecycle.  I think that would be overly complicated though.

     

    Stan

  • 2. Re: JSF Question about passing parameters (needing converters) to resource bundles from facelets...
    Andrey Kotov Newbie

    Hello!

    h:outputFormat uses MessageFormat.format() for output string. See MessageFormat api documentation for information about how can convert param.

     

    For example:

     

    label_pos_dt_dt2=xxx "{0}-{1,date,short}" / yyy "{2}-{3,date,short}"

     

                <h:outputFormat value="#{msg.label_pos_dt_dt2}" >
                    <f:param value="#{CalcBean.selectedPosDt}" />
                    <f:param value="#{CalcBean.selectedDPosDt}" />
                    <f:param value="#{CalcBean.selectedPosDt2}" />
                    <f:param value="#{CalcBean.selectedDPosDt2}" />
                </h:outputFormat>