2012-10-29 51 views
1

我想從對象列表#{item.items3}創建一組Radibuttons,並將選定對象存儲到#{cartBean.selectedChoice}中。現在我沒有真正區分<f:selectItems><ui:repeat>所需的值。我的代碼看起來如何。迄今爲止的任何明顯錯誤?如何從List創建Primefaces radioButtons?

<p:selectOneRadio id="myRadio" value="#{cartBean.selectedChoice}" layout="custom"> 
    <f:selectItems value="#{item.items3}"/> 
</p:selectOneRadio> 

<h:panelGrid columns="1"> 
    <ui:repeat var="choice" value="#{item.items3}" varStatus="choiceIndex"> 
     <p:radioButton id="choiceRadio" for=":iterateCategories:iterateItems:lightForm:myRadio" itemIndex="#{choiceIndex.index}" />#{choice.name} 
    </ui:repeat> 
</h:panelGrid> 

目前我收到以下錯誤:

20:58:52,397 INFO [javax.enterprise.resource.webcontainer.jsf.renderkit] (http-localhost-127.0.0.1-8080-1) WARNING: FacesMessage(s) have been enqueued, but may not have been displayed. sourceId=iterateCategories:0:iterateItems:2:lightForm:myRadio[severity=(ERROR 2), summary=(Conversion Error setting value '[email protected]' for 'null Converter'.), detail=(Conversion Error setting value '[email protected]' for 'null Converter'.)]

我不明白,在這裏作爲同一類的唯一對象處理,有可能是一個轉換的問題。

回答

2

JSF生成HTML。 HTML基本上是一個大字符串。因此,非字符串類型的Java對象需要轉換爲字符串。如果遇到類型不是內置轉換器(Number,BooleanEnum),也不是定製轉換器(實現Converter的類),則該對象的默認toString()實現將用於將複雜的Java對象打印到HTML輸出中。如果你的對象不具有此方法被重寫時,那麼這將是其在javadoc描述的Object#toString()的默認的實現:

The toString method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character @ , and the unsigned hexadecimal representation of the hash code of the object. In other words, this method returns a string equal to the value of:

getClass().getName() + '@' + Integer.toHexString(hashCode()) 

在您的特定情況下,所生成的HTML單選按鈕元件變成:

<input type="radio" ... value="[email protected]" /> 

(在瀏覽器中右擊頁面並選擇查看源代碼,看看它自己)

現在,這種形式提交時,必須將由request.getParameter()(其返回String!)收集的輸入值[email protected]轉換回您的自定義類型Item的具體實例。然而,由於顯然沒有轉換器被註冊爲自定義類型(錯誤消息已經提示:「空轉換器」),JSF無法將其轉換回Item,並且會拋出該轉換器異常。

你應該提供一個自定義的轉換器,它可以在Item和它的唯一String表示之間進行適當的轉換。技術ID(例如數據庫中自動生成的PK)經常被用作唯一的String表示法。然後轉換器將像下面這樣:

@FacesConverter(forClass=Item.class) 
public class ItemConverter implements Converter { 

    @Override 
    public void getAsString(FacesContext context, UIComponent component, Object modelValue) throws ConverterException { 
     // Write code to convert Item to its unique String representation. E.g. 
     return String.valueOf(((Item) modelValue).getId()); 
    } 

    @Override 
    public void getAsObject(FacesContext context, UIComponent component, Object submittedValue) throws ConverterException { 
     // Write code to convert unique String representation of Item to concrete Item. E.g. 
     return someItemService.find(Long.valueOf(submittedValue)); 
    } 

} 

或者,你可以使用JSF工具庫OmniFacesSelectItemsConverter使轉換器將改用<f:selectItem(s)>作爲轉換的基礎。這樣,您無需爲每個想要在<f:selectItem(s)>中使用的自定義Java類型創建自定義轉換器。另請參見the SelectItemsConverter showcase page

<p:selectOneRadio ... converter="omnifaces.SelectItemsConverter"> 
+0

感謝您的詳細解答,提供了自己設置轉換器所需的信息。正如我不確定,我可能需要投出哪些其他Java類型,我選擇了用於處理轉換問題的全方位替代方案。 – Lester

+0

不客氣。 – BalusC

+0

我仍然得不到的是'和''之間的差異。 ''甚至是必要的嗎? – Lester

相關問題