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

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:
Field initialized with 2012-01-01
GUI change to 2012-01-05, bean method debugs to 2012-01-01
GUI change to 2012-01-15, bean method debugs to 2012-01-05
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.
Firebug verifies, that the correct updated values are transmitted in a POST request
I'm using Primefaces 3.2 and JBoss-7.1.1-Final
Even without the p:dataTable and only using p:calendar the issue is still the same.
The wrong value of the debug output can be observed for the method-Object update(ObWithDate myO) as well for the corresponding entry in myBean.listObWithDates.
<p:ajax immediate="true" .../> does not solve this issue.
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.

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.

I ran into this issue today morning. Found that primefaces have removed the DateSelectEvent from Primefaces 3.5 . Hence had to remove the ajax event call from my calendar object. Instead retrieving the value using get..(). Please find the discussion for the same in this link below.
http://forum.primefaces.org/viewtopic.php?f=3&t=27590

Related

Validation message of an inputFile is thrown in the console [duplicate]

I created an user interface with a form to get information from user. When I submit the form, it prints the following warning in the server log:
INFO: WARNING: FacesMessage(s) have been enqueued, but may not have been displayed.
sourceId=j_idt7:j_idt11[severity=(ERROR 2), summary=(j_idt7:j_idt11: Validation Error: Value is not valid), detail=(j_idt7:j_idt11: Validation Error: Value is not valid)]
I tried to solve it, however I didn't understand how. How is this problem caused and how can I solve it?
As to the warning referring an undisplayed Validation Error: Value is not valid message, this means that you've somewhere a <h:selectXxx> component such as <h:selectOneMenu> without an associated <h:message> and therefore JSF is not able to display faces messages about conversion/validation errors directly in the UI.
In order to fix the particular warning, just add a <h:message>:
<h:selectOneMenu id="foo" ... />
<h:message for="foo" />
Note that when you're ajax-submitting the form using <f:ajax>, then you should not forgot to include the messages in the ajax-update. To start with, use render="#form" to update the entire form.
As to the concrete validation error problem which is mentioned in the message, head to the following answer: Validation Error: Value is not valid.
If you are using primefaces, dont forget to add:
<h:form>
<p:growl id="messages" showDetail="true" />
....
</h:form>
It is invalid value problem. Most probably you entered character instead of integer value. Check this and I suggest exception handling to not face these type of problems again.

METADATA parameters disappear after AJAX action

in template i'm defining a parameter
<f:metadata >
<f:viewParam name="pageKey" value="#{parameterProvider.pageKey}" />
</f:metadata>
All works fine except AJAX requests with makes this param disappear?
example ajax button in xhtml page:
Message value from server= [#{lockerBean.msg}]<br/>
<h:form>
<h:commandButton value="Ajax Action" styleClass="systemButton" >
<f:ajax execute="#all" render="#all" listener="#{lockerBean.ajaxButton(event)}"/>
</h:commandButton><br/>
</h:form>
ajax action is simply
public void ajaxButton(ActionEvent event){
msg="Ajax reload";
}
After ajax request pageKey is gone. If i set required="true" i get valudation error
j_idt3: Validation Error: Value is required. with clearly indicates, that parameter has been deleted. how can i preserve it?
I know about includeViewParams, but i don't understand this mechanism very well and it is not easy to add to all kinds of buttons and links... can i somehow force it to be passed or can i inject them somewhere in context?
EDIT:
this log might help to understand what is happening:
ParameterProvider, is sessionscoped CGI that holds pageKey. I modified getter & setter, so they log info about being called. In whole aplication getters and setters to this value is used only when applying request value in jsf lifecycle (my own code uses different methods)
14:04:06,400 OFF [ParameterProvider] (default task-12) Getting pageKey[-1] into viewParams.
14:04:06,401 OFF [ParameterProvider] (default task-12) Setting pageKey[2] from viewParams.
14:04:06,401 OFF [MultiActionLockListener] (default task-12) AJAX ACTION
14:04:06,414 OFF [ParameterProvider] (default task-12) Getting pageKey[3] into viewParams.
as you can see, before anything we have calling getter and setter for key (2 has been send from client 3 suppose to be sent to client in response)
calling another action will cause to log:
14:08:54,281 OFF [MultiActionLockListener] (default task-14) AJAX ACTION
14:08:54,294 OFF [ParameterProvider] (default task-14) Getting pageKey[7] into viewParams.
as you see, there is no getter and setter, which means pageKey is gone?

OmniFaces validateOrder disabling

I'm trying to use validateOrder component to validate two java.util.Date objects. It is similar to showcase example on this link (PrimeFaces example). Everything works perfect, but i have one question:
What if 2nd date field is not required?
In that case i'm getting nullpointer exception, and since validateOrder has "disabled" attribute, i was wondering is it worth/possible enabling/disabling it via ajax every time the 2nd date is inserted/removed. If not, i guess i'll stick to Balus' approach for JSF2.0 cross-field validation that you can read about on this link.
Let the disabled attribute check if the 2nd field is filled in. If it's not filled in, the request parameter value associated with field's client ID will be empty. Use exaclty that to let disabled attribute evaluate to true.
<p:calendar ... binding="#{endDate}" />
...
<o:validateOrder ... disabled="#{empty param[endDate.clientId]}" />
Code is as-is. No additional backing bean property necessary for binding.
See also:
How does the 'binding' attribute work in JSF? When and how should it be used?

How to know whether JSF bean setter is invoked from AJAX event or as part of form POST processing

I have a form that contains a Part Number inputText field and some other fields that represent the attributes of a part. If the user enters an existing part number, I want to populate the other fields with the part's existing attributes. The operator then has the option to change Part Number to something else (essentially creating a new part using the existing part as a template) and/or modify its attributes. I have an AJAX event defined to update the model when Part Number changes, so I can retrieve the attributes from the database:
<f:ajax event="valueChange" execute="#this" render="partlength" />
My question is this: How does the Part Number setter (e.g. setPartNumber() ) know whether it is being invoked as part of an AJAX event, in which case I want to fetch the attributes, or as part of the Update Model Values phase of the form being posted, in which case I don't? Or is there a better way to accomplish what I'm trying to do?
You can determine by PartialViewContext#isAjaxRequest() whether the current request is an ajax request or not. You can obtain the PartialViewContext by FacesContext#getPartialViewContext().
if (FacesContext.getCurrentInstance().getPartialViewContext().isAjaxRequest()) {
// The current request is an ajax request.
}
An alternative is to just do the job in the listener method of <f:ajax> instead of in the getter/setter (doing business job in getters/setters is a poor practice anyway):
<f:ajax listener="#{bean.listener}" render="partlength" />
(note that I omitted the event and execute attributes as you've used the default values already)

<ui:param> facelet tag performance

I have a file with large content to display. For example in displaying user profile, every EL expression in <h:outputText/> needs an userId as an argument to bean which is taken from the session context. I declared this userId in xhtml file as
<ui:param name="userId" value="#{currentUser.id}"/>
I am passing this userId to bean methods as
<h:outputText value="#{profile.getAddress(userId)}"/>
<h:outputText value="#{profile.getContact(userId)}"/>
<s:link>
<f:param name="userId" value="#{userId}"/>
</s:link>
I am expectiong the session variable is invoked once for a page. But each time when the userId is processed the sessiion variable is called. Is this the correct behaviour? How to optimize this?
Yes this is the correct behavior. It would be interesting to see which is faster. I would guess it is faster to inject the currentUser in your profile component, and then to retrieve the correct object from there, instead of getting the address and contact by the userId each time. (Depends if you cache it in the component or not).
However, I would try to optimize it by injecting the currentUser in the profile component. That is the standard way of doing it.

Resources