views:

917

answers:

2

I have some code! The code looks like this:

<c:forEach var="element" items="%{serviceList.getServices()}">
        <p>Hello!</p>
</c:forEach>

"serviceList" is a bean, with a method on it called getServices(). getServices() returns an ArrayList, so I naturally assumed that the above code would take the arraylist, and iterate through it putting each element of the list into the variable 'element'.

Not so! When I view the page, Hello gets printed once (the size of getServices() is 2, and I can show this directly by printing it out on the page.

The tag itself works:

<c:forEach begin="1" end="10">Hello, World!<br></c:forEach>

Prints out what you would expect. So it must be something to do with the items I'm passing in. Any help?

Oh, and this is using JSTL 1.1.2, Struts 2.1.6, and the latest version of Java.

+4  A: 

You've got a typo, you should be using ${variable name} like this:

<c:forEach var="element" items="${serviceList.services}">
  <p>Hello!</p>
</c:forEach>

And then the behavior will be as you expect.

Nick Holt
EL (as of JSP 2.1) does not let you invoke methods on instances; you can only map custom functions as static methods. (Though better method support is coming in JEE6 et al.)
McDowell
@McDowell - thanks, just copied it down from the question - I'll edit and correct it.
Nick Holt
A: 
${serviceList.services}

Your Expression Language syntax is incorrect. See the EL part of the JSP 2.1 spec for more.

McDowell