Question

Say I have the following for each in a stripes layout definition

<c:foreach items="${foo}" var="bar" >
     <s:layout-component name="whatever" />
</c:foreach>

Then when I render I do something like this

<s:layout-component name="whatever">
    //Do something with bar
</s:layout-component>

The whatever component is rendered before being placed in the layout so bar is null and it fails. Is there a way I can build a whole page before the jsp is parsed?

Was it helpful?

Solution

Seeing the absence of any answer and the question looking fairly trivial, I don't think there are many Stripes users out here. so here are my two cents:

This is definitely a scoping problem. the <s:layout-component> doesn't have access to the page/loop scope of the parent page. Similar problem exist in JSP/JSTL when you do a <jsp:include> inside a <c:forEach>. The loop variable is inaccessible in the code snippet included by <jsp:include>. But in JSP/JSTL that could be solved by passing a <jsp:param> along the <jsp:include>. I took a quick look in the Stripes documentation and I discovered a <stripes:param>. See if that helps. At least here's a JSP/JSTL based SSCCE to get the idea:

main.jsp

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<% 
    // Just for demo purposes. Do NOT use scriptlets in real work.
    request.setAttribute("items", java.util.Arrays.asList("foo", "bar", "waa"));
%>

<c:forEach items="${items}" var="item">
    <jsp:include page="include.jsp">
        <jsp:param name="item" value="${item}" />
    </jsp:include>
</c:forEach>

include.jsp

${param.item}<br>

output:

foo
bar
waa
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top