JSF 2 Scriptmanager style functionality
I need to be able to add some javascript to all ajax postback responses (PartialViewContext.isAjaxRequest == true) but I am not succeeding with any implementation I try.
I have tried implementing a
PhaseListener
and adding my script using PartialResponseWriter.insert* to add eval blocks, as well as trying to add the script by creating a script element. (Results in CDATA cannot nest, or just invalid XML)
I have tried decor开发者_如何学Goating PartialViewContextFactory to override the
PartialViewContext.processPartial
and add the script after the wrapped instance has processed it...
How should I go about adding sripts to an Ajax response? Something similar to what .NET has with Scriptmanager.registerClientScriptBlock preferably.
Thank you
Use the javascript libraries that come bundled with your JSF2 implementation (as required by the spec). Here are the API docs:
http://java.sun.com/javaee/javaserverfaces/2.0/docs/js-api/index.html
The jsf.ajax.addOnEvent
solves your problem of "adding some javascript to all ajax postback responses":
This function must accept a reference to an existing JavaScript function. The JavaScript function reference must be added to a list of callbacks, making it possible to register more than one callback by invoking jsf.ajax.addOnEvent more than once.
精彩评论