Wednesday, October 30, 2013

Body onload with PrimeFaces autoRun

To perform an action during page load, the normal practice is to invoke a javascript with the onload attribute in HTML body.

function load() {
alert("Page is loaded");
<body onload="load()">
<h1>Hello World!</h1>

With PrimeFaces <p:remoteCommand /> component, backingBean action could be called directly during page load.
by turning on the autoRun attribute to true only.
<p:remoteCommand name="testAutoRun"
    autoRun="true" />

The benefits with <p:remoteCommand /> are:
1. easy to use
2. No hassles to write any javascript
3. other pages are not affected (if facelets template is used)


1 comment:

  1. This is a good solution. However after several tests I think it is not safe for some cases.

    I've met up with exception reported about "Bean is associated with a different unfinished transaction" if using autoRun.

    It seems that the browser will run those remote commands and load the contents (including the EL variables) at the same time (using multiple different threads I think). Sometimes the getters called during resolving the EL variables will conflict with the remote command methods if they are in the same bean, which is the source of the exceptions.



Related Posts Plugin for WordPress, Blogger...