Mark Ainsworth
Mark Ainsworth

Reputation: 859

Primefaces selectOneMenu does not execute on change

I am writing a PrimeFaces application. I have the following code in my XHTML:

<p:selectOneMenu value="#{lottoCheckerBean.selectedPowerBallDrawingDate}">
    <f:selectItems value="#{lottoCheckerBean.powerBallDrawingDates}" />
</p:selectOneMenu>

I am expecting the following code to be executed in my LottoCheckerBean when a value is selected:

public void setSelectedPowerBallDrawingDate(String selectedPowerBallDrawingDate) {
    //get drawing
    PowerBallDrawing currentDrawing = null;
    for (int d = 0; d < powerBallDrawings.size(); d++) {
        if (powerBallDrawings.get(d).getDrawingDate().equals(selectedPowerBallDrawingDate)) {
            currentDrawing = powerBallDrawings.get(d);
            break;
        }
    }
    if (currentDrawing == null) {
        try {
            //create new drawing;
            currentDrawing = new PowerBallDrawing(selectedPowerBallDrawingDate);
            powerBallDrawings.add(currentDrawing);
            Arrays.sort(powerBallDrawings.toArray());
        } catch (Exception ex) {
            //will not happen so ignore
        }
    }
    this.selectedPowerBallDrawingDate = selectedPowerBallDrawingDate;
}

However, if I set a breakpoint at the beginning of the above method, the breakpoint is not reached.

What am I missing?

Upvotes: 4

Views: 6365

Answers (1)

Magnilex
Magnilex

Reputation: 11958

The code you expect to execute upon change will be called when you submit the form in which your selectOneMenu is placed. That is when the value from the selectOneMenu will be passed to your bean.

If you would want to perform something upon any other event, such as change, you need to enable ajax:

<p:selectOneMenu value="#{lottoCheckerBean.selectedPowerBallDrawingDate}"  >
    <f:selectItems value="#{lottoCheckerBean.powerBallDrawingDates}" />
    <p:ajax event="change" listener="#{lottoCheckerBean.someMethod}" />
</p:selectOneMenu>

When value is changed in the backing bean, someMethod() will be called.

I would recommend you to use setSelectedPowerBallDrawingDate(String selectedPowerBallDrawingDate) only as a setter which sets the value, not to conatain any business logic at all. Then let the method you call from <p:ajax/> do the business logic.

Upvotes: 6

Related Questions