开发者

Two collections in f:selectItems - possible?

开发者 https://www.devze.com 2023-02-18 18:14 出处:网络
I have two vectors:开发者_C百科 private Vector<City> allCities; private Vector<Address> allAddresses;

I have two vectors:开发者_C百科

private Vector<City> allCities;
private Vector<Address> allAddresses;

A City-object contains a cityId, postcode and cityname, an Address-object an addressId, street and cityId. Vector allCities can contain more Cities than used by allAddresses.

Now I want to show all the addresses in a h:selectOneListbox, but instead of the cityId there should be the postcode and the cityname, like this:

postcode, cityname, street

Is there any way to do this without changing the vectors? Maybe c:forEach is an option?

The following is not working yet, because I don't know how to concatenate or use the two vectors in f:selectItems.

        <h:selectOneListbox id="addresses"
            rendered="#{!empty customerAddresses.allAddresses}"
            required="true" requiredMessage="Please choose an address!"
            value="#{customerAddresses.addrId}" label="Addresses">
            <f:selectItems value="#{customerAddresses.allAddresses}" var="addr"
                itemLabel="#{addr.postcode}, #{addr.cityname}, #{addr.street}"
                itemValue="#{addr.addressId}" />
        </h:selectOneListbox>


You should change some of your basic settings:

First of all you should use List instead of Vector.

Then let the Address class have a member City such like this:

public class Address {
 private City city;
 private int addressId;
 private String street;

 // getters and setters
}

Then you can use it in your facelet this way:

<h:selectOneListbox id="addresses"
     rendered="#{!empty customerAddresses.allAddresses}"
     required="true" requiredMessage="Please choose an address!"
     value="#{customerAddresses.addrId}" label="Addresses">
     <f:selectItems value="#{customerAddresses.allAddresses}" var="addr"
         itemLabel="#{addr.city.postcode}, #{addr.city.cityname}, #{addr.street}"
         itemValue="#{addr.addressId}" />
</h:selectOneListbox>
0

精彩评论

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