Win a copy of Spring in Action (5th edition) this week in the Spring forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

Can we call Bean methods in javascript?  RSS feed

Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 19988
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!