JSTL String test oddity

I’ve come across this multiple times, and for some reason this always catches me out. You’d think being that EL has ‘eq’ and ‘ne’ operators that work with Strings that this should work:

<c:if test="${hello.name ne ''}">
    Hello <c:out value="${hello.name}"/>
</c:if>

This seems to make sense that it would do what you think it should do, but no, it always returns true. The code you’re looking for is using EL operator ’empty’:

<c:if test="${!empty hello.name}">
    Hello <c:out value="${hello.name}"/>
</c:if>

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.