56

I have a JSP file and in that file I am including another JSP file:

<c:forEach var="instanceVar" items="${instanceList}">
    <c:set var="instance"><jsp:include page="instance.jsp"/></c:set>
    ...
</c:forEach


In the file instance.jsp I want to use a variable instanceVar. I want to do it using JSTL. Is there any way to do this?

informatik01
  • 16,038
  • 10
  • 74
  • 104
SIGSTP
  • 1,125
  • 2
  • 9
  • 21

4 Answers4

93
<c:forEach var="instanceVar" items="${instanceList}">
    <jsp:include page="instance.jsp">
        <jsp:param name="myVar" value="${instanceVar}"/>
    </jsp:include>
</c:forEach>

In the instance.jsp

<c:out value="${param.myVar}"/>
informatik01
  • 16,038
  • 10
  • 74
  • 104
Alex
  • 11,451
  • 6
  • 37
  • 52
  • And how can I access the param.myVar in the java code in the included jsp, please? – Gangnus Aug 24 '17 at 13:27
  • 1
    The question was about JSTL. And it is better not to mix java code with JSTL. – Alex Aug 25 '17 at 03:43
  • It worked*. but in my case, I wasn't allowed to pass a list. The jsp complier raise an error saying which the params has to be static (value="some static value"). Is not to downvote it but it didn't solved my problem. – Francisco M Jan 13 '20 at 13:05
6

An alternative would be using setAttribute() and getAttribute()

SpringLearner
  • 13,738
  • 20
  • 78
  • 116
6

Another alternative is using JSTL tag c:set and request scope.

<c:set var="instance" value="${your.value}" scope="request"/>
<jsp:include page="instance.jsp"/>
Yuriy Tumakha
  • 1,450
  • 17
  • 21
1

The solution that work for me is the following

<c:set var="instance" value="${semaforoData}" scope="request"/>
<jsp:include page="semaforo.jsp"/>

in the jsp file, the code is:

<c:forEach var='itemSemaforo' items='${semaforoData}' varStatus='loopSemaforo'>
Print data
</c:forEach>
Jorge Santos Neill
  • 1,635
  • 13
  • 6
  • Worked for me and give me the chance to pass any kind of dynamic objects. It don't pass really the parameters, but, create a variable in the page/backing class which can be used in the included jsp. – Francisco M Jan 13 '20 at 13:06