开发者

c:forEach returning String instead of Object

开发者 https://www.devze.com 2023-02-21 20:39 出处:网络
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 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!


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

0

精彩评论

暂无评论...
验证码 换一张
取 消