Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Calling method during onload of jsf page

 
Jack pero
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Jack pero
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, but can we used "rendered" somwhere in this.

jack..
"find a way, or make the one"
 
John Bartlett
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

The commandButton has rendered attribute. You can use it.

For eg.


-Rajani.
 
Jolie Lee
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks, the rendered thing solved the problem..

jack
 
joshua asem
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
kumar Rathinam
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 18156
53
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic