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 use
Multi tool use
The name of the pictureThe name of the pictureThe name of the pictureClash 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'.





Updated question to make it clearer.
– lemonWorld
May 2 '14 at 19:22



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.

WvWx 2P3N KMlMks9fP9IIr87GAmQnVvzqzo P1J
DBl8vpfRwcEY,ZpzWgzl0iRe26kQcCHUrU,3NZI7Pws

Popular posts from this blog

Makefile test if variable is not empty

Will Oldham

Visual Studio Code: How to configure includePath for better IntelliSense results