Tuesday, October 30, 2007

Odd JSP EL behaviour

I just discovered an oddity in JSP Expression Language. I'm a huge fan of EL and its many uses. This is hardly a problem, but is something to keep in mind because it is an unexpected behavior.

Take:

int a = 5;
int b = 2;

System.out.println(a / b);

The println statement will output 2. Thus in EL (and JSTL) in a JSP you would expect the same integer division behavior from the following:

<var="a" value="5" />
<var="b" value="2" />

${a / b}


In the above, ${a / b} yields 2.5, behaving like floating point division rather than integer division. I can see this having its uses, but it surprised me, especially since I've been working almost exclusively in Java web technologies for the past few years. For future purposes I created an EL function so I can just call the following when I want to force integer division (where f is whatever prefix is specified for the custom taglib):

${f:integerDivision(a,b)}

0 comments: