Check empty or null JSTL c tags

Question

How can I validate if a String is null or empty using the c tags of JSTL?

Solution

You can use the empty keyword in a <c:if> for this:
<c:if test="${empty var1}">
    var1 is empty or null.
</c:if>
<c:if test="${not empty var1}">
    var1 is NOT empty or null.
</c:if>
Or the <c:choose>:
<c:choose>
    <c:when test="${empty var1}">
        var1 is empty or null.
    </c:when>
    <c:otherwise>
        var1 is NOT empty or null.
    </c:otherwise>
</c:choose>

Reference


Comments