tags:

views:

1143

answers:

2

Hello If I have a JSF backing bean return an object of type ArrayList, I should be able to use to iterate over the elements in the list. Each element contains a map and although the question of how to access the map content through JSTL has been answered here, if I pass an array of such maps, I can't find how to iterate over them and still access the map content using JSTL. There's documentation which refers to simple iterators but not to those whose items are themselves maps.

BalusC, I'm not trying to force the issue, just that I've been looking at this all day, and still cannot seem to be able to output the contents of my data structure through jsp (only on the console). This as a separate question still has merit.

If anyone can give me a simple example of how a java List is iterated over in JSP I'd be massively appreciative. Mark

+1  A: 

Mark, this is already answered in your previous topic. But OK, here it is again:

Suppose ${list} points to a List<Object>, then the following

<c:forEach items="${list}" var="item">
    ${item}<br>
</c:forEach>

does basically the same as as following in "normal Java":

for (Object item : list) {
    System.out.println(item);
}

If you have a List<Map<K, V>> instead, then the following

<c:forEach items="${list}" var="map">
    <c:forEach items="${map}" var="entry">
        ${entry.key}<br>
        ${entry.value}<br>
    </c:forEach>
</c:forEach>

does basically the same as as following in "normal Java":

for (Map<K, V> map : list) {
    for (Entry<K, V> entry : map.entrySet()) {
        System.out.println(entry.getKey());
        System.out.println(entry.getValue());
    }
}

The key and value are here not special methods or so. They are actually getter methods of Map.Entry object (click at the blue Map.Entry link to see the API doc). In EL (Expression Language) you can use the . dot operator to access getter methods using "property name" (the getter method name without the get prefix), all just according the Javabean specification.

That said, you really need to cleanup the "answers" in your previous topic as they adds noise to the question. Also read the comments I posted in your "answers".

BalusC
I'd love to clean them up, but there isn't a delete button and grep 'delete' on the full reference link of the edit page returns nothing. do please let me know how i can delete the bum amswers.
Mark Lewis
Maybe it's due to your low reputation that you're unable to delete. Best what you could do right now is to upvote the *real* answers (press the top triangle at the left hand side of the answer) and leave your answers as-is, so that the real answers get arranged higher in the list. To learn more about Stackoverflow, read the "about" and "faq" links at the top bar :)
BalusC
It is because of my low rep. but i think the fact i can't delete my own answers is a bit lame.
Mark Lewis
It's just to prevent *abuse* of Stackoverflow. Have patience, the more you post questions and accept other's answers and post *useful* answers on other's questions, the more reputation you get and the more Stackoverflow engine "trusts" you ;)
BalusC
thanks. i'm loving stack overflow. it's about the only thing i am loving at the moment.on your response, first, thanks. really. i'm going to login as mindrail now. is there anyway of importing my points?
Mark Lewis
You also need to **register** your account! Also see http://meta.stackoverflow.com/questions/37183/how-many-reputation-points-do-you-need-before-you-can-delete-your-own-question-or. Further on, as per the FAQ, you get 2 points for accepting an answer on your question (by pressing the big white checkmark on left hand side of the answer you'd like to accept -so that it turns green) and you get 10 points for every upvote you earn on your own questions and answers.
BalusC
To clarify: you don't need *any* reputation to delete your own answers. You **just** need to register.
Shog9
got it - cheers
Mark Lewis
A: 

To add to BalusC's answer, the problem might lie in how you're accessing the list itself in the JSP. Are you putting the list in the session?


JSPs and JSTL (using EL) access properties using the JavaBeans convention. This mean that if, for example, you have a Foo which contains a list Bar, then it is accessed like this:

<c:forEach items="${Foo.bar}" var="item">
...
</c:forEach>

and Foo must have an getter named getBar() - and it's all case-sensitive.

More here.

Matt Ball
I have that all setup, thanks
Mark Lewis