Why am I not able to find static methods through Expression Language

戏子无情 提交于 2019-12-25 02:41:52

问题


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()}
    


来源:https://stackoverflow.com/questions/20234337/why-am-i-not-able-to-find-static-methods-through-expression-language

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!