• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Calling method during onload of jsf page

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, but can we used "rendered" somwhere in this.

jack..
"find a way, or make the one"
 
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks, the rendered thing solved the problem..

jack
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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().
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Saloon Keeper
Posts: 27763
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic