0

Say I have a static class A which has several public final static fields.

public class Foo{
    public static final String A_STRING = "a_string";
}

And I have also some class which will set a list in session, like this:

List<Bar>barList = getBarList(); session.setAttribute(Foo.A_STRING, barList);

I want to access this list from jsp and iterate through each Bar object and output each Bar objects fields.

What I have come up with this:

<c:forEach items="${sessionScope[Foo.A_STRING]}" var="element">
<tr>
<td>${element.id}</td>
td>${element.name}</td>
...
</tr></c:forEach>

which is not working, any help will be appreciated, thanks.

run_time_error
  • 697
  • 1
  • 8
  • 22

1 Answers1

0

First, change ${sessionScope[Foo.A_STRING]} to ${sessionScope.a_string}.

Second, you have inconsistent variable name (elements and element), change either one of them to make it consistent.

<c:forEach items="${sessionScope.a_string}" var="element">
    <tr>
        <td>${element.id}</td>
        <td>${element.name}</td>
    ...
    </tr>
</c:forEach>
Jie Heng
  • 196
  • 4
  • Thanks for the reply. It does work but I do not want to hard code "a_string". Is there a way around ? The second issue is fixed. It was a typo. Thanks for noticing. – run_time_error May 17 '17 at 03:57