JSTL <c:when>和<choose>标签</choose> </c:when>

时间:2014-10-22 20:42:28

标签: jsp jstl jsp-tags

我是第一次进行JSTL编码,我在使用if else条件时面临一些问题(c:when和c:choose)

我需要的if else嵌套的伪代码是

if(${not empty properties['userrgba']}){
    <div style = "background-color: ${properties['userrgba']};">
}


    else if({not empty properties['userColor']})
    {
        if({not empty properties['userColorGradient']}){
        <div style = "background-color: ${properties['userColor']};
                      background: -webkit-gradient(linear, left top, left bottom, color-stop(50%, ${properties['userColorGradient']}));" >
        }
        else{
        <div style = "background-color: ${properties['userColor']};">
        }
} //END ELSE IF

else{
<div class = ${properties['color']}>
} //END ELSE

我有的JSTL if循环

<c:choose>
<c:when test="${not empty properties['userrgba']}">
    <div style = "background-color: ${properties['userrgba']};">
</c:when>
<c:otherwise>
    <c:choose>
        <c:when test="${not empty properties['userColor']}">
            <c:choose>
                <c:when test="${not empty properties['userColorGradient']}">
                    <div style = "background-color: ${properties['userColor']};
                    background: -webkit-gradient(linear, left top, left bottom, color-stop(50%, ${properties['userColorGradient']}));">
                </c:when>
                <c:otherwise>
                    <div style = "background-color: ${properties['userColor']};">
                </c:otherwise>
            </c:choose>
        </c:when>
    </c:choose>
<c:otherwise> //<-- this should be </c:otherwise>
<c:otherwise>
    <div class = ${properties['color']}>
</c:otherwise>

我得到的错误是

/path/to/file/file_name.jsp(43,0) The end tag "&lt;/c:choose" is unbalanced

第43行是我最后一次做的事情。我经常思考它并做了很多改动,但每次发生类似错误都是某些行或其他(与关闭选择标签或标签时相关)

有没有办法解决它?

谢谢!

1 个答案:

答案 0 :(得分:4)

更好地缩进代码,以便在这些事情的开始和结束时显而易见。没有正确缩进就无法找到问题。

以下是您的原始代码重新缩进。

<c:choose>
    <c:when test="${not empty properties['userrgba']}">
        <div style = "background-color: ${properties['userrgba']};">
    </c:when>
    <c:otherwise>
        <c:choose>
            <c:when test="${not empty properties['userColor']}">
                <c:choose>
                    <c:when test="${not empty properties['userColorGradient']}">
                        <div style = "background-color: ${properties['userColor']};
                        background: -webkit-gradient(linear, left top, left bottom, color-stop(50%, ${properties['userColorGradient']}));">
                    </c:when>
                    <c:otherwise>
                        <div style = "background-color: ${properties['userColor']};">
                    </c:otherwise>
                </c:choose>
            </c:when>
        </c:choose>
    <c:otherwise> //<-- this should be </c:otherwise>
    <c:otherwise>
        <div class = ${properties['color']}>
    </c:otherwise>
</c:choose>

当然,现在显而易见的问题是:在同一选择中有两个其他人是否有意义?