Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

How to call a Action on onchange event in a Dropdown ?

Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am using jsf and i want to call a action method on onchange event.
Can anyone give me idea how to do that...
If answer includes java scipt please give a detailed solution
Saloon Keeper
Posts: 24867
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
Onchange isn't an action, so you can't use an Action method for a change in a dropdown. However, you can use a ValueChangeListener.

Aside from the difference in what method you define, there's also a difference in what it's safe to do in a ValueChangeListener, since the phase that actually sets the updated properties in the backing bean hasn't been done yet. And if you want to sleep well at night, you won't try to update the properties yourself in the ValueChangeListener method.

You don't need JavaScript unless you want AJAX operation based on the selection changing. If you're looking at something like that, you might consider using an AJAX-aware tagset such as RichFaces, which allows you to bind an action listener to any of the JavaScript options and additionally spares you the trouble of writing and debugging AJAX JavaScript code yourself.
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is a tricky approach to realize your request.

Two steps:
1. Set the 'onchange' attribute of <h:selectOneMenu> to 'submit()'.
2. In the corresponding valueChangeListener method, invoke the action method you want to use and invoke renderResponse method. Like following:

The principle as following:
Once the selection changed, a submission will be performed. The valueChangeListener method will be invoked first. Then you can freely invoke your action as any simple, normal methods. At last invoking renderResponse method will let system directly render the response to browser, jumping over the following normal JSF phases. In the other word, other actions or actionListeners will not process any more.
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
    Bookmark Topic Watch Topic
  • New Topic