开发者

JSF combobox: display items from Collection [duplicate]

This question already has answers here: How to populate options of h:selectOneMenu from database? (5 answers) Closed 6 years ago.

I can't populate a JSF combo box with predefined data from a Java collection.

Backend, simple stub for collection:

PriceRecord pr = new PriceRecord();
pr.setTypeCode(Arrays.asList(123L,456L));

This doesn't work, the combo box remains empty:

<h:selectOneMenu value="#{price.typeCode}" var="code">
    <开发者_Python百科f:selectItem value="#{code}"/>
</h:selectOneMenu>


The code you've given doesn't seem to make a lot of sense. The idiom is more like this:

<h:selectOneMenu value="#{backingBean.typeCode}">               
    <f:selectItems value="#{backingBean.typeCodes}" var="typeCode" itemLabel="#{typeCode}" itemValue="#{typeCode}" />
</h:selectOneMenu>

Here, #{backingBean.typeCode} is the property that initially returns the single value that represents the default selected value. If it's empty, no value is initially selected. After the user submits the form, it will receive the value the user selected. For your code this would be of type Long.

#{backingBean.typeCodes} is the property that returns a collection of all the values a user can choose between. For your code this would be List<Long>.

Because your values are simple longs, the label and value is the same here. If it was a more complex object like e.g. a User, you could use something like #{user.name} for the itemLabel and #{user.id} for the itemValue. Whatever is rendered for itemValue is what is pushed into the value binding of the selectOneMenu component.

One other hint: in general you should try to avoid using the type SelectItem in your backing beans. Prefer simple domain objects and collections of them instead of JSF specific types.


I'm not sure what exactly the question is, but I can give you a very simple example how you could use the <h:selectOneMenu /> component.

price.xhtml:

<h:selectOneMenu id="priceMenu" value="#{priceBean.selectedPrice}">
    <f:selectItems value="#{priceBean.prices}" />
</h:selectOneMenu>

PriceBean.java:

..
private String selectedPrice;
..
public String getSelectedPrice() {
    return selectedPrice;
}

public void setSelectedPrice(String newPrice) {
    selectedPrice = newPrice;
}
..
public List<SelectItem> getPrices() {
    List<SelectItem> retVal = new ArrayList<SelectItem>();

    retVal.add(new SelectItem("2"));
    retVal.add(new SelectItem("4"));
    retVal.add(new SelectItem("6"));

    return retVal;
}

Further informations about the SelectItem. If you want to use a specially object directly, for example an object called Price, you have to use a converter. Here an example is shown.


Assuming JSF 2.0, try <f:selectItems> instead. Example from the official docs:

<h:selectOneListbox size="1" id="escape02" value="#{select05NoSelection.initialCollectionValues}">
  <f:selectItems value="#{select05NoSelection.hobbitList}"
                 var="n"
                 itemValue="#{n}"
                 itemLabel="#{n.bio}"
                 itemDescription="#{n.description}"
                 itemDisabled="#{n.disabled}"
                 itemLabelEscaped="true"
                 noSelectionValue="#{select05NoSelection.hobbitList[0]}"/>
</h:selectOneListbox>

In the preceding example, the value attribute on line 1 points to a Collection<HobbitBean>. HobbitBean is just a regular Java Object (POJO) that conforms to JavaBeans naming conventions for its properties. The value attribute on line 2 points to a List<HobbitBean>, though it could just as well point to a Collection, array, or javax.faces.model.DataModel. The attributes on lines 3 through 9, inclusive, leverage the fact that the value is a collection of POJOs.


To make your page just work you can use c:forEach to iterate items:

<h:selectOneMenu>
    <c:forEach items="#{price.typeCode}" var="code">
        <f:selectItem itemValue="#{code}" itemLabel="#{code}"/>
    </c:forEach>
</h:selectOneMenu>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜