Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 908
Niklas Rosencrantz Avatar asked May 24 '12 13:05

Niklas Rosencrantz


1 Answers

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
like image 92
BalusC Avatar answered Oct 05 '22 06:10

BalusC