I need to execute a JSF managed bean action method using ajax during HTML DOM load event,
similar to jQuery's $(document).ready(function()
I'm using JSF 2.0, Facelets and PrimeFaces.
Use the "hidden form" trick (actually, "hack" is given the ugliness a better wording).
Note that <h:outputScript
target="body"> automatically puts the <script> in
end of <body>,
thus a $(document).ready() or window.onload wrapper
is unnecessary. Also note the importance of triggering onclick() instead
of click() in
case of <h:commandButton>.
The onclick() immediately
invokes the onclick function
while the click() only
triggers the "click" event on the element, which is not supported in IE.
This however doesn't use JSF native jsf.ajax.request(),
instead it uses PrimeFaces native jQuery (you know, PrimeFaces is a JSF component library on top of jQuery/UI).
3. Create a custom UIComponent which
extends UICommand and
generates the necessary JSF native jsf.ajax.request() call.
You're then in essence reinventing the <p:remoteCommand> to
use native JSF ajax instead of jQuery. The JSF utility library OmniFaces has
a <o:commandScript> component
which does exactly that. See also the showcase and
to update the screen. Another property, autoRun,
allows you to use<prime:remoteCommand> without
sure the back end method is called automatically after the form is displayed. Insert the remoteCommand immediately before the closing </form> tag:
Be careful to define the update property correctly. The updated region of the
screen shouldn’t include the remoteCommand. You don’t want to get an infinite
You can call an arbitrary method of the bean. If you call <prime:remoteCommand> from
allow us to pass parameters, so a simple parameterless method will do.