4

I am passing Status object to h:commandLink value. So it is displayed on the page. The problem is, displayed string is

packages.entity.Status@db2674c8.

I created converter for Status with annotation

@FacesConverter(forClass = Status.class, value = "statusConverter")

but it doesn't work. I tried to explicitly set it:

<h:commandLink value="#{result.status}" action="/view">
    <f:converter converterId="statusConverter" />
</h:commandLink>

Then I got an error: /search-form.xhtml @57,58 <f:converter> Parent not an instance of ValueHolder: javax.faces.component.html.HtmlCommandLink@53e387f3

which is quite true, h:commandLink is not ValueHolder. Is there some way to convert value for h:commandLink?

amorfis
  • 15,390
  • 15
  • 77
  • 125

3 Answers3

13

Interesting, I'd intuitively expect it to work here, but the UICommand does indeed not extend UIOutput (while the UIInput does). It's maybe worth an enhancement request to JSF boys.

You can go around this issue by displaying it using <h:outputText>.

<h:commandLink action="/view">
    <h:outputText value="#{result.status}">
        <f:converter converterId="statusConverter" />
    </h:outputText>
</h:commandLink>

Or just without explicit <f:converter> since you already have a forClass=Status.class

<h:commandLink action="/view">
    <h:outputText value="#{result.status}" />
</h:commandLink>
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • You were faster ;-) Anyway, I missed the point of wrapping an h:outputText. That should do the trick. – tasel May 23 '11 at 14:30
  • Again something invaluable by BalusC. Works with `h:link` to display a formatted date as value. – Kawu Jun 03 '15 at 14:48
  • just noticed this is also a problem with h:link with MyFaces. Line 1080 of HtmlLinkRender.java does not invoke the converter: `Object value = output.getValue();` – Jonathan S. Fisher Feb 17 '23 at 18:10
0

Converters can not be attached to command components (h:commandLink, h:commandButton)

You could create a composite component or use a method in your backing bean for that.

tasel
  • 629
  • 5
  • 15
0

As you pointed out an h:commandLink is not a ValueHolder so it does not support a converter. The value attribute actually dictates the text that is displayed.

Converters are used to convert a value that is an Object into a String for representation in html and then on the flip side to convert that String back into an instance of an object.

In your example I'm guessing that result.status is an object which you'd like to convert to a string? If so you may just need to reference an actual String attribute of the object, like:

<h:commandLink value="#{result.status.statusMessage}" action="/view" />
Dave Maple
  • 8,102
  • 4
  • 45
  • 64