开发者

How do I display a list of Person objects in a h:dataTable?

开发者 https://www.devze.com 2023-02-21 19:46 出处:网络
I have a list of Person objects with properties like height, sex, colour, etc开发者_开发百科 in a backing bean. How do I display those objects in a h:dataTable in JSF?You need to load it in bean\'s co

I have a list of Person objects with properties like height, sex, colour, etc开发者_开发百科 in a backing bean. How do I display those objects in a h:dataTable in JSF?


You need to load it in bean's constructor or @PostConstruct and add a getter which returns it.

public class Bean {

    private List<Person> persons;

    @PostConstruct
    public void init() {
        persons = loadItSomehow();
    }

    public List<Person> getPersons() {
        return persons;
    }

    // ...
}

Then you can bind it to the value of the <h:dataTable>. During each step of the iteration, the currently iterated object (in this case, Person) is available by the name as definied in the var attribute. You can use it to access the properties. You can use <h:column> to define table columns.

<h:dataTable value="#{bean.persons}" var="person">
    <h:column><h:outputText value="#{person.height}" /></h:column>
    <h:column><h:outputText value="#{person.sex}" /></h:column>
    <h:column><h:outputText value="#{person.colour}" /></h:column>
</h:dataTable>

This will render a HTML <table> with one <tr> per Person with one <td> per <h:column>.

See also:

  • How to use h:dataTable in JSF 1.2
0

精彩评论

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

关注公众号