tags:

views:

503

answers:

1

How can I validate a String null or empty using the c tags of JSTL.

I have a variable of name var1 and I can display it, but I want to add a comparator for validate it.

<c:out value="${var1}" />

I want to validate when is different of null or empty (my values are string).

+6  A: 

You can use the <c:if> or <c:choose> 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

<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>

The ${not empty var1} can also be done by ${!empty var1}.

To learn more about those ${} things (the Expression Language, which is a separate subject from JSTL), check here.

BalusC