不理解JspTagException:“非<use> -style标签没有<choose>作为其直接父级</choose> </when>

时间:2010-09-06 15:13:02

标签: java jsp jstl nested

我一直盯着下面的标签嵌套大约一个小时,我仍然无法弄清楚为什么我一直得到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>

2 个答案:

答案 0 :(得分:15)

<c:otherwise>标记内嵌有 <c:when>个标记。这两个标签需要彼此对等。试试这个:

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

顺便说一句:您的每个alt代码都列出了两次<img>个属性。我在答案中删除了额外的那些。

答案 1 :(得分:3)

<c:otherwise><c:when><c:otherwise>应按如下方式使用:

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