Search code examples
jspjstlel

how to compare null in jstl with variable?


The value of y is null, but it always execute otherwise part. part. null is considered as not empty.

<c:choose>
  <c:when test="${empty y}">
   value=""
  </c:when>
<c:otherwise>
 value="${y}"
</c:otherwise>      


Solution

  • If y is string write your code like this

        <c:choose>
            <c:when test="${y==null || y==''}">
              value=""
            </c:when>
            <c:otherwise>
             value="${y}"
            </c:otherwise> 
        </c:choose>