Bulbasaur
Bulbasaur

Reputation: 714

Can I have JSTL tags inside the body of <jsp:include> tag

I am trying to include a file with dynamic parameters. The parameters come from an array.

This is the code I wrote:

<jsp:include page="${jspName}">
    <c:if test="${paramList != null}">
        <c:forEach var="paramPair" items="${paramList().getList()}">
            <jsp:param name="${paramPair.getName()}" value="${paramPair.getValue()"/> 
        </c:forEach>
    </c:if>
</jsp:include>

But this gives me an error:

Expecting "jsp:param" standard action with "name" and "value" attributes.

Can someone please help me figure out how I can send these parameters dynamically to the file from the parameter array?

Upvotes: 1

Views: 3861

Answers (1)

Prakash K
Prakash K

Reputation: 11698

You can't include any expression or jstl tags inside the body of a <jsp:include> tag, you can only have <jsp:param> tags inside the body, as per the JSP documentation.

To understand your requirement better, please answer this question:
Since the parameter names are also dynamic, how do you suppose to get these parameters inside the jsp file represented by ${jspName}?


Still here are few of my suggestions:
I would suggest rethink of the design and using the include directive (<%@ include file="myJsp.jsp" %>) instead of the standard action.

Or

If you want to use <jsp:include> then you do any of the following:

  • Pass two <jsp:param>; one with a comma-separated name list (name1,name2,name3) and other with a comma-separated value list (value1,value2,value3). In your inlcuded jsp ${jspName}, do some simple string manipulation to get the name and values.

    <c:set name="nameList" value="" /> 
    <c:set name="valueList" value="" /> 
    
    <c:if test="${paramList != null}">
        <c:forEach var="paramPair" items="${paramList().getList()}">
            <c:set name="nameList">${nameList},${paramPair.getName()},</c:set>
            <c:set name="valueList">${valueList},${paramPair.getValue()},</c:set>
        </c:forEach>
    </c:if>
    
    <jsp:include page="${jspName}">
        <jsp:param name="nameListToBePassed" value="${nameList}" />
        <jsp:param name="valueListToBePassed" value="${valueList}" />
    </jsp:include>
    
  • Pass one <jsp:param> with a comma-separated nameValue list like [name1=value1,name2=value2,name3=value3].

    <c:set name="nameValueList" value="" />
    
    <c:if test="${paramList != null}">
        <c:forEach var="paramPair" items="${paramList().getList()}">
            <c:set name="nameValueList">${nameValueList},${paramPair.getName()}=${paramPair.getValue()},</c:set>
        </c:forEach>
    </c:if>
    
    <jsp:include page="${jspName}">
        <jsp:param name="nameValueListToBePassed" value="${nameValueList}" />
    </jsp:include>
    

Hope this gives some direction.

Upvotes: 1

Related Questions