Pergunta

I need a clarification regarding following EL snippet in JSP

$ {5/x} : ${5/x}
$ {10/0} : ${10/0}
$ {5/2} : ${5/2}
$ {x/10} : ${x/10}
$ {5 div null} : ${5 div null}
$ {x/x} : ${x/x}
$ {0/0} : ${0/0}
$ {x/null} : ${x/null}
$ {null div x} : ${null div x}
$ {null/null} : ${null/null}

The output of above jsp would be

$ {5/x} : Infinity
$ {10/0} : Infinity
$ {5/2} : 2.5
$ {x/10} : 0.0
$ {5 div null} : Infinity
$ {x/x} : 0
$ {0/0} : NaN
$ {x/null} : 0
$ {null div x} : 0
$ {null/null} : 0

I've read that EL is intelligent enough in handling null values or any object that is not in any scope. Also, Division operator in EL follows Floating Point Arithmetic.

For any variable that is not found in any scope or for null keyword, EL puts a value 0 there. If so, why is ${x/x}, ${x/null}, ${null div x} and ${null/null} resulting in 0? My guess is, it should be NaN, as in case of ${0/0}. (Since, null or un-available variable is '0')

Note: In above snippet, x is a variable that is not available in any scope.

Please help me understand this behavior.

Thanks!

Nenhuma solução correta

Outras dicas

Hello JavaHopper and to all you other people.

I think, I found the solution.

My setup:

  • NetBeans 8.2 (Build 201609300101)
  • Java EE6
  • Apache Tomcat 8.0.27.0

and

  • tomcat-jasper-8.0.0-rc1-sources.jar

from this page: http://www.java2s.com/Code/Jar/t/Downloadtomcatjasper800rc1sourcesjar.htm

for debugging.

As you said, if both operands are "null" then you get 0 as a result.

If I let it debug, it will come to the point, where it call a method named:

public static final Number divide(final Object obj0, final Object obj1)

which can be found here:

org.apache.el.lang.ELArithmetic.divide(obj0, obj1)

and here is the complete method:

public static final Number divide(final Object obj0, final Object obj1) {
        if (obj0 == null && obj1 == null) {
            return ZERO;
        }

        final ELArithmetic delegate;
        if (BIGDECIMAL.matches(obj0, obj1))
            delegate = BIGDECIMAL;
        else if (BIGINTEGER.matches(obj0, obj1))
            delegate = BIGDECIMAL;
        else
            delegate = DOUBLE;

        Number num0 = delegate.coerce(obj0);
        Number num1 = delegate.coerce(obj1);

        return delegate.divide(num0, num1);
    }

As you can see at the beginning of the method their is a check, if both operands are null.

If yes you get 0 (ZERO) back and will show as a String on your JSP site for example.

So it has not quite to do with a mathematical rule.

Hope, this helps a little bit.

Best regards

rikku47

PS:

In short you can just call:

org.apache.el.lang.ELArithmetic.divide(null, null);
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top