Consoder the following code:
<h:commandButton value="do" action="#{testBacking.do}">
<f:ajax execute="@all" render="@all" listener="#{testBacking.listener}"/>
</h:commandButton>
I want to have a custom tag (with value based on server logic), in the Ajax response XML, something like the following:
<isValidationFailed> true </isValidationFailed>
I can use this data to re-enable the button (which was disabled when Ajax begin, to avoid double clicks) if validation is failed.
How can I achieve this (preferably without using any JSF 3rd party libraries)?
EDIT:
The example code, more precisely, should be like this:
<h:commandButton id="myButton" value="do" action="#{testBacking.do}">
<f:ajax execute="id1" render="id2 myButton" listener="#{testBacking.listener}"/>
</h:commandButton>
This is only possible with a custom
PartialViewContextwhich you load into your JSF application using aPartialViewContextFactory. The customPartialViewContextshould in turn return a customPartialResponseWriteronPartialViewContext#getResponseWriter(). In this customPartialResponseWriter, you should be able to add extensions to the XML response by callingstartExtension()andendExtension()inendDocument(). Something like:This will then end up in the XML response as
This is available and traversable by
data.responseXMLinjsf.ajax.addOnEvent()function.Here’s a full kickoff example how you could utilize it in your particular case:
MyPartialViewContextFactorywhich provides the custom partial view context:MyPartialViewContextwhich provides the custom partial response writer:MyPartialResponseWriterwhich writes<extension id="myextension">with the body as JSON):To get it to run, register the factory as follows in
faces-config.xml:Here’s how you can access, parse and use the
<extension id="myextension">in yourjsf.ajax.addOnEvent():However, your particular functional requirement can be achieved in a different, likely simpler, manner. Just let the ajax request update the button itself and let the button’s
disabledattribute evaluatetruewhen there’s means of a successful postback.