Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Including JSP page inside forEach loop

Tags:

java

jsp

I need to do for each loop and include loops content to other jsp page. Now I need to pass looped variable to other JSP page. I have tried following, but it didn't work. When I used attribute in included page, it just returned null value.

<c:forEach var="item" items="${items}" varStatus="loop">    
    <jsp:include page="/my_jsp_page.jsp" flush="true">
        <jsp:param name="item" value="${item}" />
    </jsp:include>
</c:forEach>
like image 773
newbie Avatar asked Sep 09 '10 07:09

newbie


People also ask

What is JSP include page?

The include directive is used to include the contents of any resource it may be jsp file, html file or text file. The include directive includes the original content of the included resource at page translation time (the jsp page is translated only once so it will be better to include static resource).

Which tag is used to iterate over a list of items in JSP?

JSTL foreach tag allows you to iterate or loop Array List, HashSet or any other collection without using Java code. After the introduction of JSTL and expression language(EL) it is possible to write dynamic JSP code without using scriptlet which clutters jsp pages.

What is C forEach in JSP?

Full Stack Java developer - Java + JSP + Restful WS + Spring The <c:forEach> tag is a commonly used tag because it iterates over a collection of objects. The <c:forTokens> tag is used to break a string into tokens and iterate through each of the tokens.


2 Answers

You can store the "item" into request attribute before call jsp:include

<c:set var="item" scope="request" value="${item}">

then read it from the request scope

like image 109
Boris Avatar answered Sep 21 '22 18:09

Boris


This is because jsp:param sets a request parameter, not a request attribute.

From within your included page, you'll have to refer to item like this:

${param['item']}

Note that, since we're talking about request parameters here, those parameters will always be strings. If this doesn't do it for you, you should follow @Boris' advice above.

like image 42
Isaac Avatar answered Sep 20 '22 18:09

Isaac