File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » JSF
Bookmark "Can we call Bean methods in javascript?" Watch "Can we call Bean methods in javascript?" New topic
Author

Can we call Bean methods in javascript?

Tony Johnson
Greenhorn

Joined: Apr 23, 2013
Posts: 1
Hi,

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: 15639
    
  15

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.

Customer surveys are for companies who didn't pay proper attention to begin with.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can we call Bean methods in javascript?
 
Similar Threads
Field display problem
Onload script does not work in subview page
How to stop page refresh when hit enter button from rich:inputNumberSpinner field?
rich:hotKey not worked when using jQuery?
rich:hotKey worked when using jQuery?