2016-12-26 82 views
0

我正在開發一個JSF應用程序 我有2個selectOnemenu控件並提交按鈕。 我需要禁用如果2個字段的值相等如何比較值JSF selectonemenu

<h:selectOneMenu id="from" value="#{currencyBean.history.fromCurrency}" > 
    <f:selectItems value="#{currencyBean.currency}" var="c" itemValue="#{c}" itemLabel="#{c.name}"/> 
</h:selectOneMenu> 
<p:outputLabel for="to" value="to:" /> 
<h:selectOneMenu id="to" value="#{currencyBean.history.toCurrency}" > 
    <f:selectItems value="#{currencyBean.currency}" var="c" itemValue="#{c}" itemLabel="#{c.name}"/> 
</h:selectOneMenu> 
<p:commandButton id="calculateButton" value="Convert" update=":convert :historyPanel" onstart="PF('statusDialog').show()" onsuccess="PF('statusDialog').hide()" validateClient="true" ajax="true" action="#{currencyBean.Calculate()}" /> 

我試圖用Ajax使用平變化,但每次我改變一個下拉第二drowpdown的價值成爲了backbean空的按鈕,所以我不能讀它。

這裏是我的backbean

@Named(value = "currencyBean") 
@RequestScoped 
public class CurrencyBean implements Serializable { 

    /** 
    * Creates a new instance of CurrencyBean 
    */ 
    private History history; 
    private Currency currency; 
    private Date currentDate; 
    @Inject 
    private Loginbean loginBean; 

    private List<History> historyList; 

    public List<History> getHistoryList() { 
     int userId = loginBean.getUserId(); 
     if (userId != -1) { 

      return new HistoryHelper().GetHistoryListByUser(userId); 
     } 
     return null; 
    } 

    public CurrencyBean() { 
     history = new History(); 
    } 

    public History getHistory() { 
     return history; 
    } 

    public void setHistory(History history) { 
     this.history = history; 
    } 

    public Currency[] getCurrency() { 
     return Currency.values(); 
    } 

    public Date getCurrentDate() { 
     return new Date(); 
    } 

    public void Calculate() { 
     int userId = loginBean.getUserId(); 
     if (userId != -1) { 
      new CurrencyClient().Convert(userId, this.history); 
     } 

    } 

} 

任何線索?

+0

你可以更精確和鍵入exatcly如何實現onchange方法?以及如何看起來像你的支持豆,特別是其範圍 –

+0

我已更新我的問題,以添加我的豆子。 關於onchange的實現,我只是試圖在日誌中寫入從兩個字段的值,但如果我從字段更新的值爲空,如果我更新到字段的值從零 –

+0

而且哪裏是這個onchange的實現?在selectOneMenu? onchange屬性中稱爲什麼 –

回答

2

我的假設是所有的問題都來自您的託管bean範圍。您有@Request範圍,因此您的託管bean的每個請求都將從容器中刪除,因此當您定義onchange="submit()"(這只是我的假設,因爲您尚未定義如何實現onchange屬性),並且您爲此選擇了一個selectBox組件值中的值組件被更新,但第一個仍然爲空。當您選擇從第一個selectBox更新後的第二個selectBox值不再存在時,因爲託管bean在首次請求後已被刪除。您應該嘗試更廣泛的範圍,例如@ViewScope。如果它沒有幫助,那麼將需要更多的信息,如實現onchange屬性將需要