Mark Ainsworth Mark Ainsworth - 3 months ago 21
Java Question

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

I am expecting the following code to be executed in my
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);
if (currentDrawing == null) {
try {
//create new drawing;
currentDrawing = new PowerBallDrawing(selectedPowerBallDrawingDate);
} 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?


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

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.