简体   繁体   中英

How Can I Populate JSF selectonemenu From Loop or Array

I am VERY new to JSF, and I need some help doing, what I thought would be, a very simple task. I would like to populate a selectonemenu from an array or loop or something. I have a form where I want the user to enter their DOB. All I want is a simple list where they can select from 1..31 for the day of month. I don't want to have 31

<f:selectItem value="n" />

tags. I attempted to put a "getDates" method in my backing bean, but that didn't work well. Any advice on how to do this will be greatly appreciated.

You need <f:selectItems> instead of <f:selectItem> . The <f:selectItems> can take a List<T> or T[] or even a Map<K,V> .

Eg

<h:selectOneMenu value="#{bean.selectedItem}">
    <f:selectItems value="#{bean.availableItems}" />
</h:selectOneMenu>

with

private String selectedItem;
private List<String> availableItems;

See also:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM