【问题标题】:Don't understand JspTagException: "Illegal use of <when>-style tag without <choose> as its direct parent不明白 JspTagException:“非法使用 <when> 样式的标签,而没有 <choose> 作为其直接父级
【发布时间】:2025-02-20 10:50:02
【问题描述】:

我已经盯着下面嵌套的标签大约一个小时了,但我仍然不明白为什么我一直收到JspTagException

"Illegal use of <when>-style tag without <choose> as its direct parent"

在JSTL中不允许嵌套这么深的条件标签吗?

<c:choose>
    <c:when test="${rec.image1Available}">
    <img alt="altname" src="/img1.jpg" alt="altname" />
    <c:otherwise>
    <c:choose>
        <c:when test="${rec.image2Available}">
        <img alt="altname" src="/img2.jpg" alt="altname" />
            <c:otherwise>
            <c:choose>
                <c:when test="${rec.image3Available}">
                <img alt="altname" src="img3.jpg" alt="altname" />
                    <c:otherwise>
                    <img alt="altname" src="/holder.jpg" alt="altname" />
                    </c:otherwise>
                </c:when>
            </c:choose>
            </c:otherwise>
        </c:when>
    </c:choose>
    </c:otherwise>
    </c:when>
</c:choose>

【问题讨论】:

    标签: java jsp jstl nested


    【解决方案1】:

    &lt;c:otherwise&gt; 内有 &lt;c:when&gt;&lt;c:otherwise&gt; 应按如下方式使用:

    <c:choose>
        <c:when ... >
            1st alternative
        </c:when>
        <c:when ... >
            2nd alternative
        </c:when>
        ...
        <c:otherwise>
            otherwise
        </c:otherwise>
    </c:choose>
    

    【讨论】:

      【解决方案2】:

      你有&lt;c:otherwise&gt;标签嵌套在里面 &lt;c:when&gt;标签。这 2 个标签需要相互对等。试试这个:

      <c:choose>
          <c:when test="${rec.image1Available}">
              <img src="/img1.jpg" alt="altname" />
          </c:when>
          <c:otherwise>
              <c:choose>
                  <c:when test="${rec.image2Available}">
                       <img src="/img2.jpg" alt="altname" />
                  </c:when>
                  <c:otherwise>
                      <c:choose>
                          <c:when test="${rec.image3Available}">
                              <img src="img3.jpg" alt="altname" />
                          </c:when>
                          <c:otherwise>
                              <img src="/holder.jpg" alt="altname" />
                          </c:otherwise>
                      </c:choose>
                  </c:otherwise>
              </c:choose>
          </c:otherwise>
      </c:choose>
      

      顺便说一句:您的每个&lt;img&gt; 标记中都列出了两次alt 属性。我删除了答案中多余的部分。

      【讨论】:

        最近更新 更多