简体   繁体   English

JSF。 转换h:commandLink显示的值

[英]JSF. Convert value for h:commandLink display

I am passing Status object to h:commandLink value. 我将Status对象传递给h:commandLink值。 So it is displayed on the page. 所以它显示在页面上。 The problem is, displayed string is 问题是,显示字符串是

packages.entity.Status@db2674c8 . packages.entity.Status@db2674c8

I created converter for Status with annotation 我为带有注释的Status创建了转换器

@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 然后我收到一个错误: /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 . 这是真的, h:commandLink不是ValueHolder Is there some way to convert value for h:commandLink ? 有没有办法转换h:commandLink值?

Interesting, I'd intuitively expect it to work here, but the UICommand does indeed not extend UIOutput (while the UIInput does). 有趣的是,我直觉地期望它在这里工作,但UICommand确实没有扩展UIOutput (而UIInput确实如此)。 It's maybe worth an enhancement request to JSF boys. 这可能值得JSF男孩的增强请求。

You can go around this issue by displaying it using <h:outputText> . 您可以使用<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 或者只是没有明确的<f:converter>因为你已经有了一个forClass=Status.class

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

Converters can not be attached to command components (h:commandLink, h:commandButton) 转换器无法附加到命令组件(h:commandLink,h:commandButton)

You could create a composite component or use a method in your backing bean for that. 您可以在辅助bean中创建复合组件或使用方法。

As you pointed out an h:commandLink is not a ValueHolder so it does not support a converter. 正如您所指出的那样,h:commandLink不是ValueHolder,因此它不支持转换器。 The value attribute actually dictates the text that is displayed. value属性实际上决定了显示的文本。

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. 转换器用于将作为Object的值转换为String以在html中表示,然后在另一侧将该String转换回对象的实例。

In your example I'm guessing that result.status is an object which you'd like to convert to a string? 在你的例子中,我猜测result.status是一个你想要转换为字符串的对象? If so you may just need to reference an actual String attribute of the object, like: 如果是这样,您可能只需要引用对象的实际String属性,例如:

<h:commandLink value="#{result.status.statusMessage}" action="/view" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM