【问题标题】:Can I have JSTL tags inside the body of <jsp:include> tag我可以在 <jsp:include> 标记的正文中包含 JSTL 标记吗
【发布时间】:2012-08-24 18:58:18
【问题描述】:

我正在尝试包含具有动态参数的文件。参数来自一个数组。

这是我写的代码:

<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>

但这给了我一个错误:

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

有人可以帮我弄清楚如何将这些参数从参数数组动态发送到文件吗?

【问题讨论】:

    标签: jsp jsp-tags jspinclude


    【解决方案1】:

    不能&lt;jsp:include&gt; 标签的正文中包含任何表达式或jstl 标签,您只能在正文中包含&lt;jsp:param&gt; 标签,根据JSP documentation

    为了更好地了解您的要求,请回答以下问题:
    既然参数名也是动态的,那你想怎么在${jspName}代表的jsp文件中获取这些参数呢?


    以下是我的一些建议:
    我建议重新考虑设计并使用包含指令 (&lt;%@ include file="myJsp.jsp" %&gt;) 而不是标准操作。

    或者

    如果您想使用&lt;jsp:include&gt;,请执行以下任一操作:

    • 传递两个&lt;jsp:param&gt;;一个带有逗号分隔的 name 列表 (name1,name2,name3),另一个带有逗号分隔的 value 列表 (value1,value2,value3)。在您包含的 jsp ${jspName} 中,执行一些简单的字符串操作以获取名称和值。

      <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>
      
    • 传递一个&lt;jsp:param&gt; 和一个逗号分隔的nameValue 列表,例如[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>
      

    希望这能给你一些方向。

    【讨论】:

      猜你喜欢
      • 2011-04-09
      • 2011-10-01
      • 1970-01-01
      • 2012-07-25
      • 2011-01-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多