404

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

Tom11
  • 2,199
  • 6
  • 26
  • 50
user338381
  • 4,043
  • 3
  • 14
  • 4

8 Answers8

795

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

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>

Or if you don't need to conditionally render a bunch of tags and thus you could only check it inside a tag attribute, then you can use the EL conditional operator ${condition? valueIfTrue : valueIfFalse}:

<c:out value="${empty var1 ? 'var1 is empty or null' : 'var1 is NOT empty or null'}" />

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

See also:

Community
  • 1
  • 1
BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452
  • 4
    For people who are having odd problems with the empty check, here's a fishy story with a possible cause: http://gayleforce.wordpress.com/2008/01/26/jstl-empty-operator/ – CodeReaper Jun 04 '12 at 12:26
  • 9
    Summarized: `empty` doesn't work on `Set` when using the ancient JSTL 1.0. You'd need to upgrade to JSTL 1.1 (which is from 2003 already). – BalusC Jun 04 '12 at 12:32
  • 5
    @BalusC - Does the EL `${not empty var1}` check for both empty and null simultaneously? I mean the test is evaluated to true if and only if `var1` is **not** null **and** `var1` is **not** empty. Is there no need to check for `null` separately? – Lion Nov 12 '12 at 00:53
  • @BalusC - Thank you. I had already checked what you mentioned in your answer prior to my comment though I just wanted to clarify once, since a `null` value and an empty string are treated differently at many places (almost everywhere). – Lion Nov 13 '12 at 12:01
  • 1
    is `empty` equvilant to ne ' ' – shareef Jan 17 '13 at 09:36
  • 2
    @shareef: no, it isn't. In case of `String` values, it's equivalent to `var ne null and var ne ''`. Further it also supports `Object`, array, `Collection` and `Map`. – BalusC Jan 17 '13 at 11:36
  • @BalusC `empty var1` was extremely useful instead of boolean Java. – Roman C Oct 19 '15 at 18:53
  • @BalusC If this answer https://stackoverflow.com/a/28360770/1097600 is copy pasted in the edit, please give proper attribution. Thanks. You will always be our hero. – Sorter Jul 30 '19 at 13:06
27

to also check blank string, I suggest following

<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

<c:if test="${empty fn:trim(var1)}">

</c:if>

It also handles nulls

andro83
  • 2,397
  • 1
  • 15
  • 13
7

if you check only null or empty then you can use the with default option for this: <c:out default="var1 is empty or null." value="${var1}"/>

Ankit Agarwal
  • 166
  • 1
  • 7
6

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

Here's the one liner.

Ternary operator inside EL

${empty value?'value is empty or null':'value is NOT empty or null'}
Sorter
  • 8,190
  • 5
  • 52
  • 63
3

You can use

    ${var == null}

alternatively.

Supun Dharmarathne
  • 1,044
  • 1
  • 9
  • 17
  • No, unfortunately, you can't. "" without any symbols in it is an empty string but is not null. – gdrt Dec 04 '19 at 13:56
1

Here's an example of how to validate a int and a String that you pass from the Java Controller to the JSP file.

MainController.java:

@RequestMapping(value="/ImportJavaToJSP")
public ModelAndView getImportJavaToJSP() {
    ModelAndView model2= new ModelAndView("importJavaToJSPExamples");

    int someNumberValue=6;
    String someStringValue="abcdefg";
    //model2.addObject("someNumber", someNumberValue);
    model2.addObject("someString", someStringValue);

    return model2;
}

importJavaToJSPExamples.jsp

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<p>${someNumber}</p>
<c:if test="${not empty someNumber}">
    <p>someNumber is Not Empty</p>
</c:if>
<c:if test="${empty someNumber}">
    <p>someNumber is Empty</p>
</c:if>
<p>${someString}</p>
<c:if test="${not empty someString}">
    <p>someString is Not Empty</p>
</c:if>
<c:if test="${empty someString}">
    <p>someString is Empty</p>
</c:if>
Gene
  • 9,441
  • 1
  • 60
  • 54
-1
In this step I have Set the variable first:

<c:set var="structureId" value="<%=article.getStructureId()%>" scope="request"></c:set>

In this step I have checked the variable empty or not:

 <c:if test="${not empty structureId }">
    <a href="javascript:void(0);">Change Design</a>
 </c:if>
ASR
  • 2,671
  • 3
  • 30
  • 57