Enoque Duarte
Enoque Duarte

Reputation: 689

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

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

Upvotes: 0

Views: 79

Answers (1)

Vassilis Blazos
Vassilis Blazos

Reputation: 1610

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.

Upvotes: 2

Related Questions