2013-02-21 49 views
0

我在JSF 2.0中遇到了一個簡單的問題,我需要建議什麼是更好的解決方案。所以,我.xhtml頁面上有分量有2個值:Yes和No.JSF中的值的邏輯

<h:outputLabel id=v1 value="#{someValue}"/> 
<f:selectItem itemValue="0" itemLabel="#{msg['label.no']}" /> 
<f:selectItem itemValue="1" itemLabel="#{msg['label.yes']}" /> 

我試圖做到的是獲得的F默認值:選擇基於outputLabel。 例如:如果outputLabel獲取值:業餘比默認f:selectItem是'是',否則是'否'默認。支持bean中的某些邏輯還是通過在xhtml頁面上呈現更好?

+0

爲什麼要這麼做?它看起來有點不合常規。如果可能的話,你在後臺bean的邏輯。 – 2013-02-21 14:05:50

回答

2

您需要將其設置在輸入組件的value屬性後面的屬性中。你不顯示完整的代碼,但如果它是例如<h:selectOneRadio>像這樣

<h:outputLabel for="foo" value="#{bean.label}" /> 
<h:selectOneRadio id="foo" value="#{bean.value}"> 
    <f:selectItem itemValue="0" itemLabel="#{msg['label.no']}" /> 
    <f:selectItem itemValue="1" itemLabel="#{msg['label.yes']}" /> 
</h:selectOneRadio> 

,那麼你應該在設置bean的(崗位)構造函數#{bean.value}。完整的圖片不清楚,所以下面是一個基本的開球示例,而不是一個非常適合的解決方案:

private String label; 
private Integer value; 

@PostConstruct 
public void init() { 
    label = "Amateur"; 
    value = "Amateur".equals(label) ? 1 : 0; 
}