57

E.g.

<c:if test="${post}">
    <h3>${post.title}</h3>  
</c:if>
Paul
  • 19,097
  • 13
  • 76
  • 95
Sergio del Amo
  • 74,335
  • 67
  • 149
  • 178
  • 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 – Adrien Be 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) – Adrien Be Oct 01 '14 at 08:31

2 Answers2

107

Use the empty keyword

<c:if test="${not empty post}">
   <h3>${post.title}</h3>   
</c:if>
krosenvold
  • 73,421
  • 30
  • 146
  • 205
12

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

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