views:

606

answers:

2

Hi all,

I have a for each loop that it works fine when the items property is filled using an scriplet in the following way:

<%
    List<LandingCategory> cats = beanFactory.getLandingCategories();
%>

<c:forEach var="cat" items="<%=cats%>">
    <c:out value="${cat.id}"/>    
</c:forEach>

However, when trying to filled the items list with a param specified in another jsp file, the for each will not work.

JSP1
<jsp:include page="/jsp/modules/index/index_categories.jsp">    
    <jsp:param name="categories" value="<%=cats%>"/>
</jsp:include>

JSP2
<c:forEach var="cat" items="${param.categories}">
    <c:out value="${cat.id}"/>    
</c:forEach>

The following error is thrown:

javax.servlet.jsp.el.ELException: Unable to find a value for "id" in object of class "java.lang.String" using operator "."

It seems that it is considering the objects of the items list to be Strings, but I've no clue about why this is happening.

Does anyone has any idea?

Thanks

+4  A: 

The <jsp:param> tag provides a way of emulating the sort of parameters that you would pass in from an HTTP request. As such, they are Strings. So what JSP1 is doing is taking your "cats" collection, converting it into a String (using toString()), and then passing that String as a parameter to JSP2.The foreach is then trying to iterate over that string. The Cat data structure was lost in translation.

What you need to do instead is to store the cats object as a request-scoped attribute, which will allow JSP2 to retrieve it:

<%
    List<LandingCategory> cats = beanFactory.getLandingCategories();
%>
<c:set var="cats" scope="request" value="<%=cats%>"/>
<jsp:include page="/jsp/modules/index/index_categories.jsp"/>
skaffman
A: 

I'm pretty sure that

<jsp:param name="categories" value="<%=cats%>"/>

is causing toString() to be called on cats. Use a tag file for this instead, it works better:

<yourTags:fileName cats='${cats}' />

instead.

stevedbrown