Why am I not able to find static methods through E

2019-08-16 04:31发布

问题:

I was having a class MyClass with a static method getmyStaticMethod() While trying to access this method through EL in my jsp :
${MyClass.myStaticMethod}

It was giving me unable to find the value for "myStaticMethod" in object of the class MyClass is it because the static method being at Class level and the EL looking only at the Object level is not able to find it ????

Thanks in advance. :)

回答1:

The JSP EL can't access static methods of classes.

${MyClass.myStaticMethod} means: find an attribute named "MyClass" in the page scope, then in the request scope, then in the session scope, then in the application scope and, if found, get its property named "myStaticMethod" (i.e. call the getter getMyStaticMethod() on this object).

So, as you see, it doesn't look for a class named MyClass, and doesn't call any of its static method. And there's no way to do that with the JSP EL.

EDIT:

As of version 3.0 of the expression language specification (part of Java EE 7), accessing static fields and methods is possible by

  • importing the class (or package) in the JSP and
  • using the class name followed by the method:

    ${MyClass.myStaticMethod()}