Nacho321
Nacho321

Reputation: 1991

Iterate through List<String> using JSF 2

I have this java code.

List<String> myList = new ArrayList<String>();
myList.add("Hello");
myList.add("World");

I need to loop through the list to display those values in a webpage. I thought of using a dataTable but I don't know how to retrieve each entry on the list.

Ideas? Thanks!

Upvotes: 18

Views: 41936

Answers (1)

Luiggi Mendoza
Luiggi Mendoza

Reputation: 85799

You can use <ui:repeat>:

<ui:repeat value="#{bean.myList}" var="value">
    #{value} <br />
</ui:repeat>

If you're not sure if you should use <h:dataTable> or <ui:repeat>, you can check an example that mkyong provides here: JSF 2 repeat tag example

In short: <h:dataTable> renders a <table> HTML component, while <ui:repeat> gives you the flexibility to choose how to display the data.

Upvotes: 46

Related Questions