58

E.g.

<c:if test="${post}">
    <h3>${post.title}</h3>  
</c:if>
Paul
  • 19,704
  • 14
  • 78
  • 96
Sergio del Amo
  • 76,835
  • 68
  • 152
  • 179
  • 1
    although if `title` does not exist (ie. the property does not belong to this variable/bean), you want to catch the `javax.el.PropertyNotFoundException` , see question `Checking attribute exists in JSP` on http://stackoverflow.com/questions/2522562/checking-attribute-exists-in-jsp – Adriano Oct 01 '14 at 08:24
  • Your question should probably be reformulated as "check if an attribute is **set**" (not null and not an empty string) – Adriano Oct 01 '14 at 08:31

2 Answers2

108

Use the empty keyword

<c:if test="${not empty post}">
   <h3>${post.title}</h3>   
</c:if>
krosenvold
  • 75,535
  • 32
  • 152
  • 208
12

You can also use '!' instead 'not' :

<c:if test="${!empty post}">
    <h3>${post.title}</h3>
</c:if>
hejiaming007
  • 341
  • 3
  • 12