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


Jhonnathan Emilio Cardona Saineda
Ranch Hand

Joined: Jan 24, 2012
Posts: 38

what i need is that when a user choose a country, automaticaly can add a cities based in the country, but I don't find an attribute in the selectOneMenu as the action of the comandButton that triggers an event and so i can load the cities.

How can launch an event from the selectOneMenu when chosen country?

<h:selectOneMenu id="m">
<f:selectItems value="#{ejemploBB.myList1}" />

<h:selectOneMenu id="m1">
<f:selectItems value="#{ejemploBB.myList2}" />


Sorry for my English, I'll improve
Bharat Kasodariya
Ranch Hand

Joined: Aug 19, 2011
Posts: 36
you can use 'valueChangeListener' attribute.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 17417

The reason you couldn't find anything is that until JSF 2, there wasn't any pretty way to do that.

JSF is built on HTTP and HTTP is not a continuous-conversation protocol. In other words, when you change control values, no actual network communications occur between client and server until an actual form submit is done. That is, the user clicks on a commandButton or commandLink.

So in order to make a menu selection submit a form, you'd need to add an "onchange" event attribute to the parent selection list. That onchange javascript would then "click the submit button".

But submitting an entire page is slow and ugly.

To make things more friendly, what's needed is AJAX. AJAX allows submitting just part of the page and in response updating only part(s) of the page. In particular, you'd want to update the child selection control based on the selection of the parent selection control.

There are 3 ways to do this. One is to use a set of custom extension tags with AJAX support in them such as RichFaces or IceFaces. That was the cleanest way to do things in JSF 1.x. In JSF2, the core tagset collection added an AJAX tag. The third way is the least-desirable way, since it involves writing your own JavaScript AJAX code, although use of a packaged solution such as jQuery can help.

I have found that the easiest way to get a dependent list control to follow changes to its parent selection is to write the "get" accessor for the child control's selectItems in a way where if there's no current list of selectitems, a list is created based on whatever the parent selection is. So what I do is make the value "set" method for the parent invalidate the child selectItem list, which causes a new, updated selectItem list to be built when the child control is re-rendered. It's also important when doing this to reset the current selection value for the child control, since the original selection value may not be referring to a valid value in the new selection list.

An IDE is no substitute for an Intelligent Developer.
Jhonnathan Emilio Cardona Saineda
Ranch Hand

Joined: Jan 24, 2012
Posts: 38

Tim¡¡¡¡¡¡¡ thank you very much, your explanation helped me
I agree. Here's the link:
subject: selectOneMenu
It's not a secret anymore!