Is It REALLY Empty?

Ryan Sonnek bio photo By Ryan Sonnek

JSP is so damn ghetto.

Who would have thought that JSP is so incredibly incompetent that it can screw up one of the easiest operations? All I want to do is determine whether or not a collection is empty. Simple right? Well, obviously this is something too complex to be addressed by JSP.

So, here’s my super simple usecase that actually works. The JSP is pretty straight forward.

<c:if test="${not empty myObject.entries}">
  The Collection is Not Empty!  The world is coming to an end!
</c:if>

And there’s nothing special about my object.

public class MyObject {
  private Collection entries = new ArrayList();

  public Collection getEntries() {
    return entries;
  }
}

Now, guess what happens when I want my entries to ensure only one entry is added. You’d expect to change the colleciton from an ArrayList to a HashSet, and you’d be good to go right? Well, you’d be dead wrong.

public class MyObject {
  private Collection entries = new HashSet();

  public Collection getEntries() {
    return entries;
  }

}

For some unknown reason, JSTL can not work with sets to determine empty collections. Lists are fine, and even Maps are supported, but not Sets. Way to go JSTL. You’ve proven yet again that JSP is worthless.

Luckily, I stumbled across this blog that verifies my problem, so I didn’t spend all day digging into this.