Resetiing <h:selectOneMenu> and <h:selectOneListBox>

I don't know how to reset an <h:selectOneMenu> or an <h:selectOneListBox> back to default values, and would most appreciate some advice on this.

In my xhrml code I have the following:

and in my bean I have the following:

I am reading in data from a file, and I want to keep that data in memory, but I don't know how to reset the menu so that by default the first item is displayed, as it is when the page is first visited. The ActionEvent just clears the menu completely, which is not what I want.

Once the page has been submitted and the page is revisited, the previously selected menu item is shown, and a simple <h:commandButton type="reset" Value="Reset"/> only resets the menu back to the previously selected value, which may not necessarily be the first item. Everything else is working corfrectly, I just don't know how to reset.

I also tried the JavaScript statement

using a simple reset button with JavaScript attached, but that does not work either. Yes, the right hand side of the expression picks up the first entry, but this is overridden from the value picked up from the bean.

Several olther places in my application I have menus and list boxes.

Some help getting resolved with be most appreciated.

Christopher Sharp
Ranch Hand

Joined: Dec 12, 2007
Posts: 155

posted Dec 30, 2012 13:33:32

0

In my bean I forgot to copy over here the all important declaration of physicsMap as follows:

Elsewhere in my code I have declared a couple of arrays of my own objects.

Somehow JSF must keep an index of the currently selected item in the menu displayed, but I do not know where that index is, and how to access and change it back to the beginning of the LinkedHashMap, so would appreciate some help on this.

The value of a f:selectitems tag should be an ordered collection (array or List) of SelectItem objects. Note the word "ordered". A Map is a random-access collection, so there is no guaranteed repeatability of ordering. The SelectItem class encapsulates a label/value pair corresponding to the <OPTION> HTML tag body and VALUE= attributes. If no explicit value is given, the label serves as both label and value.

When JSF rendered a SelectOneMenu control, it sets the selected item to be whichever SelectItem object in its SelectItem list has a value equal to the current value= of the selectOneMenu tag. If that property has a value that's not in the list of SelectItems, then the first item in the list will be used as a default and/or a ValidationError will be produced.

Customer surveys are for companies who didn't pay proper attention to begin with.

Christopher Sharp
Ranch Hand

Joined: Dec 12, 2007
Posts: 155

posted Dec 31, 2012 12:28:24

0

Many thanks for your advice, but for now I found a way of clearing a bean by using methods of the type:

where resetOptions() calls resetIndex() and any other methods that resets beans, including beans with properties accessed from the page currently displayed.

I have a number of beans accessed from various pages, and some beans access the properties of others, so having them with different scopes does not work very well. The easiest is to have them all session scoped, then when I want to clear them, I call methods like here. This has the advantage of clearing up all pages, even if it means having to reload some of the data from files. It also means that when a user has submitted a page for some calculations and revisits the page, the original data is still there, and can be reused with a few changes. If the user wants to clear the page he can.