2011-05-23 38 views
4

我正在將Status對象傳遞給h:commandLink值。所以它顯示在頁面上。問題是,顯示的字符串是JSF。轉換h:commandLink的值顯示

[email protected]

我創建轉換器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 <f:converter> Parent not an instance of ValueHolder: [email protected]

這是真的,h:commandLinkValueHolder。有什麼方法可以將值轉換爲h:commandLink

回答

10

有趣的是,我會直覺地期待它在這裏工作,但UICommand確實不延伸UIOutput(而UIInput確實)。這對JSF男孩來說可能是值得一提的。

您可以通過使用<h:outputText>顯示它來解決此問題。

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

或者只是沒有明確<f:converter>既然你已經有了一個forClass=Status.class

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

你是快;-)不管怎樣,我錯過了包裝的H點:的outputText。這應該夠了吧。 – tasel 2011-05-23 14:30:40

+0

是的。這是一個很好的解決方案。 – 2011-05-23 14:33:45

+0

是的,它工作得很好:)謝謝! – amorfis 2011-05-23 20:16:51

0

轉換器不能連接到命令組件(H:commandLink,H:的commandButton)

您可以創建一個複合組件或在你的後臺bean中使用一個方法。

0

正如您所指出的,h:commandLink不是ValueHolder,因此它不支持轉換器。值屬性實際上決定了顯示的文本。

轉換器用於轉換一個值,該值是一個對象到用於在HTML表示的字符串,然後在另一面,以該字符串轉換回成一個對象的一個​​實例。

在您的例子我猜result.status是一個對象,你想轉換爲字符串?如果是這樣,你可能只需要引用對象的實際String類型的屬性,如:

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