views:

635

answers:

1

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?

A: 

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
BalusC
That's a good stab for sure, but the problem here is that at the time I'm calling the layout the item does not yet exist so i can't pass it as parameter.Thanks for giving it a go.I was going to make this question non stripes specific as it is just a scope question but I can't think of a time when I'd do something equivalent with jsp:includes. I suspect that this is just not possible, but if anyone does no of a way to include jsp without first evaluating the expressions on the page let us know.
Ollie Edwards