ajaxjsf-2primefacesseam3

Primefaces p:calendar with p:ajax value not updated (one step delay)


I want to call a bean after a date is selected in a p:calendar. In my setup I have a @Named @ViewScoped Bean, the class ObWithDate is a @Entity with a date field validFrom.

<h:form id="fUser">
  <p:dataTable id="dt" var="cum" value="#{myBean.listObWithDates}">
    <p:column>
      <p:calendar id="cValidFrom" value="#{cum.validFrom}">  
        <p:ajax event="dateSelect" listener="#{myBean.update(cum)}"
                update=":fUser:dt"/>  
      </p:calendar>  
    </p:column>
  </p:dataTable>
</h:form>

The good thing is, that the bean method update(ObWithDate myO) is called with the correct object each time I select a date. The bad thing is, that myO.validFrom does not have the correct value. It's always one step behind:

This issue drives me crazy, I've found several issues with not correctly updated values and also some about p:calendar. Most of them suggest using the attribute selectListener="#{calendarBean.handleDateSelect}" with a handleDateSelect(DateSelectEvent event) method. But I want to call my method since I want to pass the current instance of the variable.

Update MattHandy helped me a lot, the resulting question How to get the corresponding list item in the ajax call inside the beans method? is formulated in the followup JSF p:calendar in p:dataTable: How to get the row of p:ajax dateSelect event.


Solution

  • This is most likely a lifecycle issue. The setter for your field is called before your listener executes. You should not use the listener to set your value.

    The el expression on your listener is evaluated at render response phase of the previous request thus it holds the cum value "one step behind".

    You should update your custom object from the setter of the calendar's date value.