Search code examples
jspjstlel

If a model attribute does not exist, when used with JSTL c:when test for a boolean value, does it always evaluate to false?


So for example, if in a jsp we put:

<c:choose>
    <c:when test="${missingAttribute}">
         ...do something
    </c:when>
    <c:otherwise>
        ...something else
    </c:otherwise>
</c:choose>

If 'missingAttribute' does not exist, will the otherwise block always be executed? In other words, do missing attributes evaluate to false?


Solution

  • Yes it will evaluate to false. EL expression specified in an attribute value is processed differently depending on the attribute's type category defined in the TLD. In core when tag, it should evaluate to boolean. In core out tag to String, etc.,

    In your scenario, you need to have c:choose tag enclosing c:when and c:otherwise.

        <c:if test="${somecondition}">
            <c:choose>
                <c:when test="${missingAttribute}">
                    ...do something
                </c:when>
                <c:otherwise>
                    ...something else
                </c:otherwise>
            </c:choose> 
        </c:if>
    

    c:otherwise runs only if all of the c:when conditions are evaluated to 'false'.