user3861559
user3861559

Reputation: 993

JSTL <c:when> and <choose> tags

I am doing JSTL coding for the first time and I am facing some issues using the if else conditions ( c:when and c:choose )

The psuedo code of if else nesting I need is

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

The JSTL if loop I have is

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

The error I am getting is

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

Line 43 is the where I am doing for the last time. I pondered over it a lot and made a lot of changes but every time similar error occurred is some line or the other ( related to closing of choose tag or when tag)

Is there a way I can fix it?

Thanks!

Upvotes: 1

Views: 2709

Answers (1)

developerwjk
developerwjk

Reputation: 8659

Indent your code better so its obvious where these things begin and end. Its impossible to find the problem without indenting properly.

The below is your original code reindented.

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

Of course, the obvious question now is: Does it make sense to have two otherwises in the same choose?

Upvotes: 5

Related Questions