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 Resetiing <h:selectOneMenu> and <h:selectOneListBox> Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSF
Bookmark "Resetiing <h:selectOneMenu> and <h:selectOneListBox>" Watch "Resetiing <h:selectOneMenu> and <h:selectOneListBox>" New topic
Author

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

Christopher Sharp
Ranch Hand

Joined: Dec 12, 2007
Posts: 154
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: 154
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.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16054
    
  21

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: 154
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.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16054
    
  21

I've got a nasty feeling that you're doing this the hard way and that proper scoping and design of the backing beans would eliminate the need for explicit JSF-specific code.

But you're the one who has to live with it, so good luck!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Resetiing <h:selectOneMenu> and <h:selectOneListBox>