This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes JSF and the fly likes Calling method during onload of jsf page Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » JSF
Bookmark "Calling method during onload of jsf page" Watch "Calling method during onload of jsf page" New topic
Author

Calling method during onload of jsf page

Jack pero
Greenhorn

Joined: Sep 09, 2005
Posts: 27
Hi,
i want to call a mehtod when the page loads in JSF. Could anyone help me out for this..
thanks..
vin
Jolie Lee
Ranch Hand

Joined: May 08, 2005
Posts: 87
you can try body on~load calls a javascript method, then in the javascript method calls a commandbutton which is hidden. that commandbutton can then call a method in your backing bean.



Jys<br /><a href="http://jy-s.com" target="_blank" rel="nofollow">http://jy-s.com</a><br /> <br />Trying my very best to learn java, please forgive me if i'm asking some really stupid questions.
Jack pero
Greenhorn

Joined: Sep 09, 2005
Posts: 27
Thanks, but can we used "rendered" somwhere in this.

jack..
"find a way, or make the one"
John Bartlett
Ranch Hand

Joined: Jan 25, 2006
Posts: 116
Hey,

Can you not use shale for this, implementing shales view controller means you can override methods such as init(), preprocess() and prerender().

John
[ February 14, 2007: Message edited by: John Bartlett ]
rajani varma
Ranch Hand

Joined: Dec 21, 2006
Posts: 30
Hi,

The commandButton has rendered attribute. You can use it.

For eg.


-Rajani.
Jolie Lee
Ranch Hand

Joined: May 08, 2005
Posts: 87
yes, commandButton can use rendered but you still need to trigger the button action so that it can calls your backing bean method. rendered can't triggered the action.
Jack pero
Greenhorn

Joined: Sep 09, 2005
Posts: 27
thanks, the rendered thing solved the problem..

jack
joshua asem
Greenhorn

Joined: Aug 21, 2008
Posts: 13
How does the rendered thing solved the problem? Can someone explain the details on this?
Just wondering if there is any way without calling <onload="test()">, to solve this requirement.
Can anyone give a pointer on this? Thanks.

Cheers,
Joshua
kumar Rathinam
Greenhorn

Joined: Jul 18, 2011
Posts: 3
Jolie Lee wrote:you can try body on~load calls a javascript method, then in the javascript method calls a commandbutton which is hidden. that commandbutton can then call a method in your backing bean.


function test(){
document.getElementById('formName:buttonId').click();
}

<body on~load="test();">

<h:commandButton action="#{backingBean.methodName}" id="buttonId" style="display: none"/>
</body>


Hi i am trying to use the function on body onload .its hitting more then once test();
Test function is called endless loop...

Thanks
kumar
Stefan Frings
Greenhorn

Joined: Sep 24, 2011
Posts: 1
The hidden button does not work beause the action handler answers with a page, which is the same page in this case. So the page will get re-loaded and because the onload script clicks the button again, the page gets reloaded in an endless loop.

Sometimes the solution is so easy to oversee it:





This calls the init method when the page gets loaded.

If you use the older JSF 1.2 release, then write #{myBean.init} and name the method getInit().
Rodney Hirokane
Greenhorn

Joined: Dec 04, 2009
Posts: 8
Stefan Frings wrote:The hidden button does not work beause the action handler answers with a page, which is the same page in this case. So the page will get re-loaded and because the onload script clicks the button again, the page gets reloaded in an endless loop.

Sometimes the solution is so easy to oversee it:





This calls the init method when the page gets loaded.

If you use the older JSF 1.2 release, then write #{myBean.init} and name the method getInit().


I could not get this solution to work in my case. I created a web page where the submit button executes myBean.method. I need to eventually remove the Submit button and for myBean.method to be executed when the page is loaded. Here is the code for the submit button

<h:commandButton action="#{agencySecurityProfileBean.submit}" value="Submit" styleClass="button" />

Besides including a hidden command button as listed in the previous posts is there a better alternative to resolving this issue or has anyone gotten the simple solution as quoted in this post to work?

Much appreciated,

Rod
Samuel Younge
Greenhorn

Joined: Aug 26, 2012
Posts: 1
Stefan Frings wrote:The hidden button does not work beause the action handler answers with a page, which is the same page in this case. So the page will get re-loaded and because the onload script clicks the button again, the page gets reloaded in an endless loop.

Sometimes the solution is so easy to oversee it:





This calls the init method when the page gets loaded.

If you use the older JSF 1.2 release, then write #{myBean.init} and name the method getInit().



***

I was able to get the above post to work but I ended up putting the call in between the <head> tags and modifying it slightly.
Section of my index.xhtml


Method in my Managed Bean Controller msbController.java

Notice the addition of parenthesis after the init method name.
Works like a champ thanks Stefan!

Also, some people may do fine just adding the code they want to execute on page load in the managed bean controller's constructor method.

Samuel
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16019
    
  20

Welcome to the JavaRanch, Samuel!

What you have done is what we call "awakening a zombie" - posting to a thread that has been dormant for so long that most of the people involved have probably moved on to other projects and often other employers. We'd prefer that you simply start a new thread rather than do this, as it's less confusing.

BTW. JSF backing beans are not Controllers, they are Models.


Customer surveys are for companies who didn't pay proper attention to begin with.
 
 
subject: Calling method during onload of jsf page