David
David

Reputation: 33

c:forEach returning String instead of Object

I have a request scoped Struts 1 Action which contains a list of custom POJO objects from my application. Inside the action, I use request.setAttribute("myForm", myForm) to set the form value. When I reach the JSP page, I try to use a c:forEach loop to iterate over the elements in the list and print out a property of each element. However, the c:forEach loop always throws the following error:

javax.servlet.jsp.JspException: An error occurred while evaluating custom action attribute "value" with value "${listObject.name}": Unable to find a value for "name" in object of class "java.lang.String" using operator "." (null)

My ActionForm has the following entities:

private List<MyCustomObjects> myList;
public List<MyCustomObjects> getMyList() { return myList; }
public void setMyList(List<MyCustomObjects> myList) { this.myList = myList; }

In the JSP page, I have the following loop:

<c:forEach var="listObject" items="myForm.myList">
    <c:out value="${listObject.name}" />
</c:forEach>

Does anyone see what I did wrong or why this doesn't work? Thanks!

Upvotes: 3

Views: 3331

Answers (1)

BalusC
BalusC

Reputation: 1108712

You need to wrap the expression in ${}.

<c:forEach var="listObject" items="${myForm.myList}">
    <c:out value="${listObject.name}" />
</c:forEach>

Otherwise it's indeed treated as a String, namely one with the literal value "myForm.myList".

Upvotes: 5

Related Questions