How to Compare the values JSF selectonemenu

456 views Asked by At

I am developing a JSF application I have 2 selectOnemenu controls and submit button. I need to disable the button if the values of 2 fields are equal

 <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()}" />

I tried to use onchange with ajax but everytime I change one dropdown the value of the second drowpdown became null in the backbean so I cannot read it.

Here is my 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);
        }

    }

}

any clue ?

1

There are 1 answers

0
Artur Skrzydło On BEST ANSWER

My assumption is that all of your problems come from your managed bean scope. You have @Request scope so every request your managed bean will be removed from container, thus when you define onchange="submit()" (this is only my assumption because you haven't define how you implement onchange attribute) and you select value from one selectBox component values for this component is updated but the first one is still null. When you select second selectBox value updated from first selectBox doesn't exists anymore as managed bean has been removed after first request. You should try with wider scope for instance @ViewScope. If it doesn't help then further informations like implementation onchange attribute will be needed