If I have a
ArrayList<Person> persons
How do I access it in EL?
<c:foreach items="${what goes here??}" var="person">${person.title}</c:foreach>
If I have a
ArrayList<Person> persons
How do I access it in EL?
<c:foreach items="${what goes here??}" var="person">${person.title}</c:foreach>
names
should be in the set as attribute available for the viewIf you are using Servlets or action class for creating your list and then forwarding it to your JSP, then you must be having following line in your servlet or action class.
The expression
${foo}
uses behind the scenesJspContext#findAttribute()
which searches for attributes inPageContext
,HttpServletRequest
,HttpSession
andServletContext
in this order by theirgetAttribute("foo")
method wherebyfoo
from${foo}
thus represents the attribute name"foo"
and returns the first non-null object.So, if you do in a servlet
And call this servlet by URL, then you'll be able to iterate over it in
page.jsp
as follows:The above is also equally valid when you put it in the session scope instead
or even in the application scope
EL will for
title
in${person.title}
implicitly look for a public instance (not static!) method prefixed withget
inPerson
class like below:The field
title
does not necessarily need to exist in the class (so you can even return a hardcoded string and keep using${person.title}
), and it does not necessarily need to be an instance field (so it can also be a static field, as long as the getter method itself isn't static).Only
boolean
(notBoolean
!) getters have a special treatment; EL will implicitly look for a public method prefixed withis
. E.g. for a${person.awesome}
:See also: