Difference between ${empty var} nd ${var.isEmpty()} evaluation on a String object

StackOverflow https://stackoverflow.com/questions/23497024

  •  16-07-2023
  •  | 
  •  

Question

Is there any difference on evaluating a String object on JSTL between

<c:if test="${empty aString}"></c:if>

and

<c:if test="${aString.isEmpty()}"></c:if>

?

EDIT: Both works, .isEmpty() gives an anotation error on Spring Tool Suite

Était-ce utile?

La solution

The code as you have it, both are calculated as false because you have a whitespace inside your test " "

test=" true" <!-- condition is calculated as false -->
test="true" <!-- condition is true -->

Moreover to call method from jstl you have to call it withour get or is prefix. e.g. ${var.empty} calls isEmpty or getEmpty of var object.

Edit

It will be better approach ${empty var}, because it's true either var is '' or null. empty covers and null conditions.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top