0

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. :)

user2131465
  • 97
  • 2
  • 3
  • 11

1 Answers1

3

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()}
    
Community
  • 1
  • 1
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • 1
    This answer is no longer correct as of EL 3.0 which added support for static methods and static fields. EL 3.0 is part of Java EE 7 so you'll need a container that supports EL 3.0 such as Tomcat 8. – Mark Thomas Nov 27 '13 at 10:43
  • @MarkThomas: thanks for the info. I missed that change in EE 7. I edited my answer. – JB Nizet Nov 27 '13 at 11:59