JSTL / EL equivalent of testing for null and list size?

el, jakarta-ee, java, jsp, jstl

Solution

Use the `empty` keyword, it checks both nullness and emptiness.

<c:if test="${not empty sokandeList}">
    ...
</c:if>

Note that when your intent is to iterate over the list using `<c:forEach>` then it may be good to know that it already won't run when the provided `items` is `empty`. If the `<c:forEach>` is directly surrounded by this check, then this check is entirely superfluous.

See also:

- Java EE 6 tutorial - operators in EL

- Java EE 6 tutorial - examples of EL expressions

Problem

Possible Duplicate: Evaluate empty or null JSTL c tags I'm refactoring scriptlets to JSTL and EL and I would like to know how to write the following in JSTL / EL: ``` if(sokandeList != null && sokandeList.size() > 0) { %> ... ``` I don't know how to test for null and AFAIK EL can only access getters in this context so I must add a method getSize() to the sokandeList class. Correct? What should the JSTL / EL expression look like? Thanks for any help

Original source

Related problems