Questions tagged [selectonemenu]

JSF tag to create a single-select menu.

A <h:selectOneMenu> is a JSF UI component which generates a HTML <select> element which allows you to bind the selected value to a managed bean property.

Basic example

You can use <f:selectItem> to define menu options which get generated as HTML <option> elements.

View:

<h:form>
    <h:selectOneMenu value="#{bean.selectedItem}">
        <f:selectItem itemValue="#{null}" itemLabel="-- select one --" />
        <f:selectItem itemValue="foo" itemLabel="Foo label" />
        <f:selectItem itemValue="bar" itemLabel="Bar label" />
        <f:selectItem itemValue="baz" itemLabel="Baz label" />
    </h:selectOneMenu>
    <h:commandButton value="Submit" action="#{bean.submit}" />
</h:form>

Model:

private String selectedItem; // +getter +setter

public void submit() {
    System.out.println("Selected item: " + selectedItem);
}

The itemValue is what will be set as managed bean property. The itemLabel is what will be displayed as option label in the UI. If the itemLabel is omitted, then it will default to the same value as itemValue.

You can preselect an item by giving the selectedItem a value in the bean's (post)constructor. E.g.

@PostConstruct
public void init() {
    selectedItem = "bar";
}

This will show the menu with "Bar label" preselected.

Dynamic list

You can use <f:selectItems> to display a list which is dynamically populated in the backing bean. You can use javax.faces.model.SelectItem to represent a pair of item value and label.

View:

<h:form>
    <h:selectOneMenu value="#{bean.selectedItem}">
        <f:selectItem itemValue="#{null}" itemLabel="-- select one --" />
        <f:selectItems value="#{bean.availableItems}" />
    </h:selectOneMenu>
    <h:commandButton value="Submit" action="#{bean.submit}" />
</h:form>

Model:

private String selectedItem; // +getter +setter
private List<SelectItem> availableItems; // +getter (no setter necessary)

@PostConstruct
public void init() {
    availableItems = new ArrayList<SelectItem>();
    availableItems.add(new SelectItem("foo", "Foo label"));
    availableItems.add(new SelectItem("bar", "Bar label"));
    availableItems.add(new SelectItem("baz", "Baz label"));
}

The availableItems can also be a SelectItem[]. If you omit the item label and thus can use the item value as both option value and option label, then you can also use a List<String> or String[] instead.

private String selectedItem; // +getter +setter
private List<String> availableItems; // +getter (no setter necessary)

@PostConstruct
public void init() {
    availableItems = Arrays.asList("foo", "bar", "baz");
}

A Set is also supported.

private String selectedItem; // +getter +setter
private Set<String> availableItems; // +getter (no setter necessary)

@PostConstruct
public void init() {
    availableItems = new LinkedHashSet<String>(Arrays.asList("foo", "bar", "baz"));
}

Note that you should use a LinkedHashSet to maintain the insertion order, or a TreeSet if you want automatic sorting by value. A HashSet is by nature unordered.

Dynamic list with groups

You can use SelectItemGroup to group items.

private String selectedItem; // +getter +setter
private List<SelectItem> availableItems; // +getter (no setter necessary)

@PostConstruct
public void init {
    availableItems = new ArrayList<SelectItem>();

    SelectItemGroup group1 = new SelectItemGroup("Group 1");
    group1.setSelectItems(new SelectItem[] {
        new SelectItem("Group 1 Value 1", "Group 1 Label 1"),
        new SelectItem("Group 1 Value 2", "Group 1 Label 2"),
        new SelectItem("Group 1 Value 3", "Group 1 Label 3")
    });
    availableItems.add(group1);

    SelectItemGroup group2 = new SelectItemGroup("Group 2");
    group2.setSelectItems(new SelectItem[] {
        new SelectItem("Group 2 Value 1", "Group 2 Label 1"),
        new SelectItem("Group 2 Value 2", "Group 2 Label 2"),
        new SelectItem("Group 2 Value 3", "Group 2 Label 3")
    });
    availableItems.add(group2);
}

Here's how it look like in the browser (note that Group 1 and Group 2 are unselectable):

alt text

Dynamic map

The <f:selectItems> also supports a Map<K, V> where K is to be represented as item label and V is to be represented as item value.

View:

<h:form>
    <h:selectOneMenu value="#{bean.selectedItem}">
        <f:selectItem itemValue="#{null}" itemLabel="-- select one --" />
        <f:selectItems value="#{bean.availableItems}" />
    </h:selectOneMenu>
    <h:commandButton value="Submit" action="#{bean.submit}" />
</h:form>

Model:

private String selectedItem; // +getter +setter
private Map<String, String> availableItems; // +getter (no setter necessary)

@PostConstruct
public void init() {
    availableItems = new LinkedHashMap<String, String>();
    availableItems.put("Foo label", "foo");
    availableItems.put("Bar label", "bar");
    availableItems.put("Baz label", "baz");
}

If you want to swap the K and V in the view side so that you can populate the item values and labels the other way round like follows

@PostConstruct
public void init() {
    availableItems = new LinkedHashMap<String, String>();
    availableItems.put("foo", "Foo label");
    availableItems.put("bar", "Bar label");
    availableItems.put("baz", "Baz Label");
}

Then you first need to make sure that your environment supports EL 2.2 (part of Servlet 3.0, i.e. target server is Tomcat 7, Glassfish 3, etc and web.xml is declared conform Servlet 3.0), then you can iterate over Map#entrySet() in <f:selectItems> as follows:

<f:selectItems value="#{bean.availableItems.entrySet()}" var="entry" 
    itemValue="#{entry.key}" itemLabel="#{entry.value}" />

Note that you should use a LinkedHashMap to maintain the insertion order, or a TreeMap if you want automatic sorting by map key. A HashMap is by nature unordered.

Dynamic list of objects

Since JSF 2.0, you can also use a List<Bean> or Bean[] instead where Bean is just an arbitrary javabean class. Assuming that you've a Country bean with two String properties code and name and you want to store the code as selected item value and display the name as item label, then you can use the var attribute of <f:selectItems> without the need to convert it to List<SelectItem> or SelectItem[].

View:

<h:form>
    <h:selectOneMenu value="#{bean.countryCode}">
        <f:selectItem itemValue="#{null}" itemLabel="-- select one --" />
        <f:selectItems value="#{bean.countries}" var="country" 
            itemValue="#{country.code}" itemLabel="#{country.name}" />
    </h:selectOneMenu>
    <h:commandButton value="Submit" action="#{bean.submit}" />
</h:form>

Model:

private String countryCode; // +getter +setter
private List<Country> countries; // +getter (no setter necessary)

@EJB
private DataService service;

@PostConstruct
public void init() {
    countries = service.getCountries();
    // If this is a static list, you'd rather put this
    // in a separate application scoped bean instead.
}

public void submit() {
    System.out.println("Selected country code: " + countryCode);
}

Storing objects as item value

From the example in the previous section, you can also set the whole Country as a managed bean property. You'd only need to create a converter which converts between Country and String. A more concrete example can be found in those answers:

Validation Error: Value is not valid

This dreaded error will occur when roughly the following happens under the JSF covers during submitting the form:

String submittedValue = request.getParameter(component.getClientId());
Converter converter = component.getConverter();
Object selectedItem = (converter != null) ? converter.getAsObject(context, component, submittedValue) : submittedValue;

boolean valid = false;

for (Object availableItem : bean.getAvailableItems()) {
    if (selectedItem.equals(availableItem)) {
        valid = true;
        break;
    }
}

if (!valid) {
    throw new ValidatorException("Validation Error: Value is not valid");
}

You see, it will occur when the selected item does not equal any of the available items. This can in turn have 3 causes:

  1. The list of available items from bean has changed during the form submit.
  2. The equals() method of the object type behind the item is missing or broken.
  3. If any used, the converter has returned the wrong item or even null on getAsObject().

See also this answer: Validation Error: Value is not valid.

Static list of enums

You can also use an enum as available items and selected item.

View:

<h:form>
    <h:selectOneMenu value="#{bean.selectedItem}">
        <f:selectItem itemValue="#{null}" itemLabel="-- select one --" />
        <f:selectItems value="#{bean.availableItems}" />
    </h:selectOneMenu>
    <h:commandButton value="Submit" action="#{bean.submit}" />
</h:form>

Model:

public enum Option {
    FOO, BAR, BAZ;
}

private Option selectedItem; // +getter +setter

public void submit() {
    System.out.println("Selected item: " + selectedItem);
}

public Option[] getAvailableItems() {
    return Option.values();
}

See also this answer how to change and internationalize item labels: How to use enum values in f:selectItem(s).

Populate a child menu

You can use the JSF 2.0 <f:ajax> tag to dynamically populate a child menu depending on the selected item of the current menu. Here's an example which does that for countries-cities.

View:

<h:selectOneMenu value="#{bean.country}" converter="countryConverter">
    <f:selectItem itemValue="#{null}" itemLabel="-- select one --" />
    <f:selectItems value="#{bean.countries}" var="country" 
        itemValue="#{country}" itemLabel="#{country.name}" />
    <f:ajax listener="#{bean.loadCities}" render="cities" />
</h:selectOneMenu>
<h:selectOneMenu id="cities" value="#{bean.city}" converter="cityConverter">
    <f:selectItem itemValue="#{null}" itemLabel="-- select one --" />
    <f:selectItems value="#{bean.cities}" var="city" 
        itemValue="#{city}" itemLabel="#{city.name}" />
</h:selectOneMenu>

Model in @ViewScoped bean:

private Country country; // +getter +setter
private City city; // +getter +setter

private List<Country> countries; // +getter
private List<City> cities; // +getter

@EJB
private DataService service;

@PostConstruct
public void loadCountries() {
    countries = service.getCountries();
}

public void loadCities() {
    cities = service.getCities(country);
}
634 questions
0
votes
1 answer

Displaying a Hashtable in a selectedOneMenu list

i have a Hashtable i want to display it in a selecteOneMenu any idea how to do it ? itemCat = new Hashtable(); itemCat.put(0,"Functional"); itemCat.put(1, "Interoperability"); itemCat.put(2, "Load"); itemCat.put(3,…
Amira
  • 3,184
  • 13
  • 60
  • 95
0
votes
3 answers

How to change the list in a SelectOneMenu, if the value is changed in another?

I want to change the SelectItem[] array in the second SelectOneMenu, if the value is changed in the first one. Is that possible?
Daniel Szalay
  • 4,041
  • 12
  • 57
  • 103
0
votes
2 answers

How to get selected item from in javascript

I'm trying to get the selected item from my selectOneMenu onchange but it's not working. Every time I change the drop down and select another option, the console on my web browser says The value of the property 'getOffset' is null or undefined, not…
Zack
  • 5,108
  • 4
  • 27
  • 39
0
votes
2 answers

Apache myfaces renders datatable before processing ajax event

I have a selectOneMenu which controls what should be shown in the datatable. The code is as below (I have removed all unnecessary staff):
flyasfish
  • 119
  • 2
  • 3
  • 11
0
votes
2 answers

p:ajax isn't calling bean method

I have a simple form which should call the method countryChanged() in my Login-Bean.
0
votes
1 answer

JSF / Primefaces: Pre Select an option in a OneMenu

I have a simple OneMenu in JSF: @ManagedBean @ViewScoped public class ProductBean { ... protected static Map priceTypes; ... getter & setter }
Markus Schulte
  • 4,171
  • 3
  • 47
  • 58
0
votes
2 answers

How to make Select one menu Unselectable/Unchangeable

How to make a unselectable/unchangeable. but still it should be submitted. It should be shown only for user reference. If we use disabled=true it will not be submitted. otherthan disabled how to make select one menu unchangeable ?
Raju Boddupalli
  • 1,789
  • 4
  • 21
  • 29
0
votes
3 answers

Accessing JSF SelectItem.Label and Value from XHTML without selectOneMenu

I'm struggling with a little internationalization issue in one of my apps. The story goes like this: I have a datatable which displays records, and a selectOneMenu in order to select a column of the dataTable to be filtered for. The selectOne is fed…
KB22
  • 6,899
  • 9
  • 43
  • 52
0
votes
1 answer

selectOneMenu null when submit form from inside dataTable with commandButton Primefaces 3.4

I have selectOneMenu in footer of my dataTable and i am trying to submit a bunch of footer items to backing bean to save them.Please see image to understand. Problem is when i am submitting with commandButton which is also in footer p:selectOneMenu…
smubasher
  • 43
  • 1
  • 9
0
votes
1 answer

PrimeFaces selectOneMenu not disabling

I have a selectOneMenu and trying to disable on page load. I have a couple other inputText elements that do become disabled when the onLoad is triggered. When searching the web I did find a couple other questions around this topic but no answers.…
Joseph Vance
  • 59
  • 1
  • 3
  • 10
0
votes
1 answer

h:SelectOneMenu onchange event not working

i have two oneSelectMenu loaded with default values based on login Details,then second selectonemenu should load value based on first selectonemenu's onchangeEvent menu.i tried to clear the default value before onchange event but the value remains…
Bernad Ali
  • 1,699
  • 6
  • 23
  • 29
0
votes
2 answers

Retrieving the value of the selected item in a SelectOneMenu List

i just want to retrieve the login of the selected User in the SelectOneMenu List here's the code : My xhtml page :
Amira Manai
  • 2,599
  • 8
  • 40
  • 60
0
votes
1 answer

drop down menu for jsf in OneToOne relation

I want to use a SIMPLE drop down menu for a OneToOne/ManyToOne relation in jsf Person.java package com.aminpy.create; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import…
Amin Oruji
  • 74
  • 1
  • 3
  • 11
0
votes
0 answers

setting field from SelectOneMenu; JSF 1.2

I have a problem related to setting property by selecting idem in SelectOneMenu. This is it: in presentation layer I have:
greengold
  • 1,184
  • 3
  • 18
  • 43
0
votes
1 answer

ice selectonemenu is not ordered

I am using jsf2.0 with icefaces3. I have a selectonemenu populated with a map object and my map has a list of currencies. When i run my jsp i see that my dropdown is populated, but the list of currencies are not ordered in the manner in which i…
ZEE
  • 381
  • 1
  • 6
  • 20