File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JSF and the fly likes Can we call Bean methods in javascript? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSF
Bookmark "Can we call Bean methods in javascript?" Watch "Can we call Bean methods in javascript?" New topic

Can we call Bean methods in javascript?

Tony Johnson

Joined: Apr 23, 2013
Posts: 1

I am new to java and jsf. previously used to work in .net 3.5. I am trying to figure out how can i invoke a bean method from javascript. Here is the problem.

I have a richfaces tabPanel on a jsf page having a switchType="ajax". There are several tabs in this tab panel. What i want to accomplish is that when a tab is clicked or enetered the backing bean initialization method is invoked. The problem is whenever a tab is clicked all methods for different tabs are invoked and data is fetched for all tabs. So i thought i would use a javascript function to be called on "ontabenter" method which will in turn call the relevant backing bean method but i dun know if its the right approach and if it is how to do it (dun know the syntax). i am complete blank.

Here is my code:

With the above code all the methods are invoked every time page loads or a tab is clicked\enetered. Hope i made my problem clear. Waiting for help
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 17410

OK, first thing, and this applies to ALL J2EE, not just JSF: When you have a Model/View/Controller architecture, you shouldn't be putting code in the View template. Doing so violates the Separation of Concerns aspect, and in more practical terms it makes the application a lot more expensive (and less fun) to support.

Aside from that, the whole "JSP" concept went out the window in JSF2, so all those JSP elements and scriptlets lose support entirely. Scriptlets will get you yelled at even in our JSP forum, anyway. They were a hack until better things came along.

JavaScript runs on the Client. The backing beans are on the server. To make the twain meet, you need AJAX. You could roll your own AJAX from raw JavaScript, but if you value your sanity, I recommend using an AJAX support library such as jQuery. Many of the JSF extension frameworks use jQuery internally, as a matter of fact.

An IDE is no substitute for an Intelligent Developer.
I agree. Here's the link:
subject: Can we call Bean methods in javascript?
It's not a secret anymore!