If a model attribute does not exist, when used with JSTL c:when test for a boolean value, does it always evaluate to false?
Multi tool useClash Royale CLAN TAG#URR8PPP
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?
2 Answers
2
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'.
If missingAttribute is a collection list or set in that case you need to check if it is empty or not test="${not empty missingAttribute}" else if that attribute is set then it will show error "boolean test on List".
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Updated question to make it clearer.
– lemonWorld
May 2 '14 at 19:22