We have an ajax navigation menu which updates a dynamic include. The include files have each their own forms.
<h:form>
<h:commandButton value="Add" action="#{navigator.setUrl('AddUser')}">
<f:ajax render=":propertiesArea" />
</h:commandButton>
</h:form>
<h:panelGroup id="propertiesArea" layout="block">
<ui:include src="#{navigator.selectedLevel.url}" />
</h:panelGroup>
It works correctly, but any command button in the include file doesn't work on first click. It works only on second click and forth.
I found this question commandButton/commandLink/ajax action/listener method not invoked or input value not updated and my problem is described in point 9.
I understand that I need to explicitly include the ID of the <h:form>
in the include in the <f:ajax render>
to solve it.
<f:ajax render=":propertiesArea :propertiesArea:someFormId" />
In my case, however, the form ID is not known beforehand. Also this form will not be available in the context initally.
Is there any solution to the above scenario?
Thanks to BalusC since his answer is really great (as usual :) ). But I have to add that this approach does not work for ajax requests coming from RichFaces 4. They have several issues with ajax and one of them is that the JSF-ajax-handlers are not being invoked. Thus, when doing a rerender on some container holding a form using RichFaces-components, the fixViewState-function is not called and the ViewState is missing then.
In the RichFaces Component Reference, they state how to register callbacks for "their" ajax-requests (in fact they're utilizing jQuery to hook on all ajax-requests). But using this, I was not able to get the ajax-response which is used by BalusC's script above to get the ViewState.
So based on BalusC's fix, i worked out a very similar one. My script saves all ViewState-values of all forms on the current page in a map before the ajax-request is being processed by the browser. After the update of the DOM, I try to restore all ViewStates which have been saved before (for all forms which are missing the ViewState now).
Move on:
Since I am not an JavaScript-expert, I guess that this may be improved further. But it definitely works on FF 17, Chromium 24, Chrome 12 and IE 11.
Two additional questions to this approach:
Is it feasible to use the same ViewState-value again? I.e. is JSF assigning the same ViewState-value to each form for every request/response? My approach is based on this assumption (and I have not found any related information).
Does someone expect any problems with this JavaScript-code or already ran into some using any browser?
You can use the following script to fix the Mojarra 2.0/2.1/2.2 bug (note: this doesn't manifest in MyFaces). This script will create the
javax.faces.ViewState
hidden field for forms which did not retrieve any view state after ajax update.Just include it as
<h:outputScript name="some.js" target="head">
inside the<h:body>
of the error page. If you can't guarantee that the page in question uses JSF<f:ajax>
, which would trigger auto-inclusion ofjsf.js
, then you might want to add an additionalif (typeof jsf !== 'undefined')
check beforejsf.ajax.addOnEvent()
call, or to explicitly include it byNote that
jsf.ajax.addOnEvent
only covers standard JSF<f:ajax>
and not e.g. PrimeFaces<p:ajax>
or<p:commandXxx>
as they use under the covers jQuery for the job. To cover PrimeFaces ajax requests as well, add the following:Update if you're using JSF utility library OmniFaces, it's good to know that the above has since 1.7 become part of OmniFaces. It's just a matter of declaring the following script in the
<h:body>
. See also the showcase.