Calling method with null parameters in EL ends up in 0 being passed instead of null
Asked Answered
R

1

10

I have a POJO:

public class Foo {
    public String getValue(Integer arg0, BigDecimal arg1) {...}
}

I put it as model-parameter from Spring MVC into JSP, and try to use it:

<c:set var="ans" value="${foo.getValue(null, null)}"/>

But in getValue method

arg0 = 0
arg1 = 0

instead of expected

arg0 = null
arg1 = null

I tried to run it on Tomcat 7.0.40 and on jetty 9.0.3

Is it some Tomcat bug, or It is right way EL works? How can I call method with null parameters in EL?

Update 1: Several sources, and documentation (http://tomcat.apache.org/tomcat-7.0-doc/config/systemprops.html) says that adding JVM property

-Dorg.apache.el.parser.COERCE_TO_ZERO=false

solves it.

But when I set it, nothing happen. I've set it correct

System.getProperty("org.apache.el.parser.COERCE_TO_ZERO")` 

returns "false" in runtime)

May be Tomcat need some other settings to change to use this param...

Revers answered 22/5, 2013 at 10:42 Comment(4)
there is something that does not convince me with this <c:set var="ans" value="${foo.getValue(null, null)}"/>. While using JSTL you can't call method in that way. You can access attributes instead ${foo.value} and the corresponding getter will be called behind the scenes. Besides, you cannot pass parameters because it is assumed that a getter is parameterlessBabysit
@maVVamaldo, In EL 2.2 it is possible.Revers
Having the same issue. Set the COERCE_TO_ZERO property both in Tomcat7 catalina.properties file and in ServletContextLoader and didn't work.Pissarro
@azgolfer, this is still actual issue. If you'll find some solution (useing glassfish instead of Tomcat doesn't counts), please let me know.Revers
S
-1

As far as I know, when you use Null, any program is made to initialize the Null value to a default value of the primitive. That means that int values will always have 0, double 0.0, boolean will be false etc.

What you could do, is to make some additional tests and verify whether the values are null at the beginning of your method, and in case they are, to make some additional tests.

Stormproof answered 5/8, 2013 at 10:39 Comment(1)
Integer and BigDecimal inicialize as null, but not as 0. Just Tomcat EL interpretator make them 0. So in foo.getValue(...) I can get only 0.Revers

© 2022 - 2024 — McMap. All rights reserved.