How to access objects in EL expression language ${}
Asked Answered
T

3

9

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>
Tomblin answered 22/3, 2011 at 5:21 Comment(0)
E
20

The expression ${foo} uses behind the scenes JspContext#findAttribute() which searches for attributes in PageContext, HttpServletRequest, HttpSession and ServletContext in this order by their getAttribute("foo") method whereby foo from ${foo} thus represents the attribute name "foo" and returns the first non-null object.

So, if you do in a servlet

ArrayList<Person> persons = getItSomehow();
request.setAttribute("persons", persons); // It's now available by ${persons}
request.getRequestDispatcher("/WEB-INF/persons.jsp").forward(request, response);

And call this servlet by URL, then you'll be able to iterate over it in page.jsp as follows:

<c:foreach items="${persons}" var="person">
    ${person.title}
<c:forEach>

The above is also equally valid when you put it in the session scope instead

request.getSession().setAttribute("persons", persons);

or even in the application scope

getServletContext().setAttribute("persons", persons);

EL will for title in ${person.title} implicitly look for a public instance (not static!) method prefixed with get in Person class like below:

public String getTitle() {
    return title;
}

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 (not Boolean!) getters have a special treatment; EL will implicitly look for a public method prefixed with is. E.g. for a ${person.awesome}:

public boolean isAwesome() {
    return awesome;
}

See also:

Edinburgh answered 22/3, 2011 at 13:39 Comment(1)
Thank you, especially the behind the scenes information as that really really helped me.Tomblin
B
1
<c:forEach var="item" items="${names}"> ${item.title}  </c:forEach>

names should be in the set as attribute available for the view

Bunin answered 22/3, 2011 at 5:24 Comment(5)
when you say "set" are you refering to <c:set>?Tomblin
you can set it in no. of ways. but it should be available to that pageBunin
Note that the syntax is wrong. You seem to have copypasted from Ashish.Edinburgh
@Edinburgh agree about syntax. and did you check time log about copying stuff :)Bunin
Sorry, I didn't expect that you was the first who made the mistake :) (it is still incorrect though)Edinburgh
C
0

If 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.

ArrayList<Person> names = "get from somewhere";

request.setAttribute("personNames",names);  

<c:foreach var="item" items="${personNames}"> ${item.title} </c:foreach>
Cami answered 22/3, 2011 at 5:32 Comment(3)
If I were to put this in the session would it be any different?Tomblin
@robertlewis2001 You will have to fetch the list from the session Scope from instead of the request scope. It should become items="{sessionScope.personNames}".Cami
Note that the syntax is wrong. As to the last comment, it's not necessary to explicitly mention the scope.Edinburgh

© 2022 - 2024 — McMap. All rights reserved.