Wednesday, 27 January 2016

JSTL transforms null BigDecimal to 0 and null String to ""

Was scratching my head why I was getting zero for a BigDecimal that I was returning as null, then realised I'd forgotten about JSTL behaviour that cleans up nulls to nicer values.

So anything extending type Numeric that is null gets shown as 0.

And anything that is a null String becomes a blank String.

As mentioned here:

After a bit of trial and error I discovered that the JSP tab library framework does a little bit of magic on known classes, such as Strings and Numericobjects. Classes of Numeric types that are null get converted to 0 andStrings get converted into the empty string ""

I discovered that if you change your internal value to anjava.lang.Object instead of a java.math.BigDecimal the magic doesn't know what to do and just passes null to your class.