Do 'is' boolean methods work in JSPs with JSTL?

When coding in JSTL you often want to use conditional logic (I'm thinking and the like) to be able to structure your page correctly. Auto-generated getter & setter methods will normally create isX() methods for the getters of boolean values (at least in Eclipse it does) but can you access this method directly from JSTL without writing a getX() version?

Does JSTL support 'is' boolean methods though? Ask a random poll of Java we developers and you will get conflicting answers so I decided to investigate, want the short answer?

YES - JSTL does support accessing isX() methods directly as if you were accessing a getX() method, but only if the return type of the isX() method is a primative boolean. If you return an object of any kind (such as Boolean isObjectBooleanTrue()) then JSTL fails to find the method and will give you a rather nasty JSP exception:

javax.el.PropertyNotFoundException: The class 'com.andrewbeacock.BooleanTest' does
not have the property 'objectBooleanTrue'.
So yes, 'is' methods work in JSTL but make sure you ONLY return primitive booleans from them.

1 comment:

cowboy said...

The JavaBean standard naming conventions only specify the use of the 'is' prefix for primitive boolean returning methods.

So it's not surprising that you get a javax.el.PropertyNotFoundException for a JSP as the property is resolved using the java.beans.Introspector class (see javax.el.BeanELResolver in the JSP API).