简体   繁体   English

如何使用1到31之间的值填充selectOneMenu列表

[英]How to populate selectOneMenu list with values from 1 to 31

I need to create a selectOneMenu list for the day of birth. 我需要为出生日创建一个selectOneMenu列表。 I need something like this: 我需要这样的东西:

for(int i=1;i<32;i++)
system.out.println(i);

How can I achieve this? 我怎样才能做到这一点?

What you need is to use f:selectItems . 你需要的是使用f:selectItems There is also a good wiki page at StackOverflow. StackOverflow上还有一个很好的维基页面

And by the way, to select a date, you could use a date picker of a rich component library like PrimeFaces or RichFaces. 顺便说一句,要选择日期,您可以使用PrimeFaces或RichFaces等丰富组件库的日期选择器

One way of doing that is creating a list in the backing bean with the values and return them in view using selectIems. 一种方法是使用值在backing bean中创建一个列表,并使用selectIems将它们返回到视图中。 For example: 例如:

@ManagedBean
public class ManagedBean{
    private ArrayList list;


    @PostConstruct
    public void init(){
        for(int i=1; i<32;i++)
            list.add(i);
    }

    //getter and setter
}

in the view: 在视图中:

<h:selectOneMenu value="#{managedBean.someValue}">
     <f:selectItems value="#{managedBean.list}" var="day" itemValue="#{day}" itemLabel=#{day}/>
</h:selectOneMenu>

Without backing bean: 没有支持bean:

<h:selectOneMenu value="#{...}"  >              
    <c:forEach var="i" begin="1" end="31">
        <f:selectItem itemLabel="#{i}" itemValue="#{i}" />       
    </c:forEach>
</h:selectOneMenu>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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