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 Resetting <h:selectOneMenu> Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » JSF
Bookmark "Resetting <h:selectOneMenu>" Watch "Resetting <h:selectOneMenu>" New topic
Author

Resetting <h:selectOneMenu>

Christopher Sharp
Ranch Hand

Joined: Dec 12, 2007
Posts: 154
Hello Ranchers

I have another question on JSF that I need help on. I have a drop-down menu that is populated using a map, and works correctly. However, I cannot find a way of resetting it so that when the same page is revisited, the menu returns to the first entry in the Map (itemValue, ItemLabel). In fact in general, how do you specify programatically which item you want selected by default?

The xhtml code is here:

and the relevant part of my bean is here:

Because the bean is session scoped, the previous values in it are kept when the page is visited again, and I would be most grateful to know how to specify which is the default item selected.

The method initUserInputs() is called when the user clicks the command button and some additional information is displayed.

I would be most grateful for any help.
Volodymyr Levytskyi
Ranch Hand

Joined: Mar 29, 2012
Posts: 505
    
    1

Hello Christopher!

<h:selectOneMenu id="countryMenu" binding="#{registerBacking.countryMenu}" value="#{registerBacking.registerUser.country}"
title="Type your country here" style="float: left;">
<f:selectItem itemValue="" itemLabel="Select Country" />
<f:selectItems value="#{registerBacking.countrySelectItems}" />
<f:ajax execute="@this" event="blur" render="registerForm:countryMsg" />
<f:ajax execute="@this" event="change" render="registerForm:regionGroup" listener="#{registerBacking.showRegions}" />
</h:selectOneMenu>
For me this is default selected option:
<f:selectItem itemValue="" itemLabel="Select Country" />

And I have view scoped backing bean so if page is reloaded my bean is recreated and ALL data backed in viewscoped bean gets back into original state.
If I need some properties to be among multiple pages I store them in SessionScoped bean, like logged in user.
In session scoped bean I have:
@Produces
@LoggedIn
public User getCurrentUser() {
if (currentUser == null) {
Principal principal = FacesContext.getCurrentInstance().getExternalContext().getUserPrincipal();
if (principal != null) {
currentUser = getUserFacade().getUserByEmail(principal.getName());
}
}
return currentUser;
}
In view scoped bean I inject it simply

It is impossible to code my code with button 'code'.


True person is moral, false is right!
Christopher Sharp
Ranch Hand

Joined: Dec 12, 2007
Posts: 154
Hello Volodymyr,

Many thanks for your reply, and I put in <f:selectItem> in the xhtml code here:

and made no other changes to the code, including the bean. The page loads for the first time showing "Select Filter Set" in the menu, but if I change it then revisit the page, whatever I changed it to continues to be displayed, it does not go back to "Select Filter Set", unless I do it manually.

I want to keep the rest of the code as much as possible the way it is, and keep my bean session sccoped, so I still need to know a simple way of setting the menu to some default value when the page is visited again.
Volodymyr Levytskyi
Ranch Hand

Joined: Mar 29, 2012
Posts: 505
    
    1

To control selected value of h:selectonemenu you should specify its value attribute (assign it to what you need).
You should do that on every page load because your data must have view scope.

But you can simply put all view scoped data into view scoped bean and multipage data into session scoped bean. And no pain with data scopes.
And it is more sufficient to keep data in narrower scope than in wider.
Christopher Sharp
Ranch Hand

Joined: Dec 12, 2007
Posts: 154
I got MyFilterBean to work in request scope, but it only works if I do not attempt to access it from my master bean which is session scoped. My master bean is used to get data from other beans and send the data out to an output file. It would complicate matters to have different beans writing data to the same file.

Another issue is that MyFilterBean reads data in from a file and initializes a set of objects. Having it session scoped means it does it once rather than each time the page is visited, which is obviously more efficient, so for these reasons I want to keep it session scoped.

What you seem to suggest is to have a separate bean that is view scoped and displays the data, then on submit send that data to my master bean.
 
GeeCON Prague 2014
 
subject: Resetting <h:selectOneMenu>