JSF。转换 h:commandLink 显示的值
我将 Status
对象传递给 h:commandLink 值。所以页面上就显示出来了。问题是,显示的字符串是
packages.entity.Status@db2674c8
。
我为带有注释的 Status
创建了转换器
@FacesConverter(forClass = Status.class, value = "statusConverter")
,但它不起作用。我尝试显式设置它:
<h:commandLink value="#{result.status}" action="/view">
<f:converter converterId="statusConverter" />
</h:commandLink>
然后出现错误: /search-form.xhtml @57,58
这是非常正确的,h:commandLink
不是 ValueHolder
。有没有办法转换 h:commandLink
的值?
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
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
有趣的是,我直觉地期望它在这里工作,但是 UICommand 确实没有扩展 UIOutput(而 UIInput 可以)。也许值得向 JSF 男孩提出增强请求。
您可以通过使用
显示它来解决此问题。或者只是没有明确的
因为你已经有一个forClass=Status.class
Interesting, I'd intuitively expect it to work here, but the
UICommand
does indeed not extendUIOutput
(while theUIInput
does). It's maybe worth an enhancement request to JSF boys.You can go around this issue by displaying it using
<h:outputText>
.Or just without explicit
<f:converter>
since you already have aforClass=Status.class
转换器不能附加到命令组件(h:commandLink、h:commandButton)
您可以为此创建一个复合组件或使用支持 bean 中的方法。
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.
正如您所指出的,h:commandLink 不是 ValueHolder,因此它不支持转换器。 value 属性实际上决定了显示的文本。
转换器用于将对象值转换为字符串以在 html 中表示,然后再将该字符串转换回对象实例。
在您的示例中,我猜测 result.status 是您想要转换为字符串的对象?如果是这样,您可能只需要引用对象的实际 String 属性,例如:
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: