Evaluate empty or null JSTL c tags

2019-01-01 06:27发布

How can I validate if a String is 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 to validate it.

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

I want to validate when it is null or empty (my values are strings).

标签: jsp jstl el
8条回答
几人难应
2楼-- · 2019-01-01 06:57

You can use

    ${var == null}

alternatively.

查看更多
君临天下
3楼-- · 2019-01-01 06:58

This code is correct but if you entered a lot of space (' ') instead of null or empty string return false.

To correct this use regular expresion (this code below check if the variable is null or empty or blank the same as org.apache.commons.lang.StringUtils.isNotBlank) :

<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
        <c:if test="${not empty description}">
            <c:set var="description" value="${fn:replace(description, ' ', '')}" />
            <c:if test="${not empty description}">
                  The description is not blank.
            </c:if>
        </c:if>
查看更多
登录 后发表回答