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.

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

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"
    action="#{myBean.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)


Done!!

LinkWithin

Related Posts Plugin for WordPress, Blogger...