开发者

calling method of class in jsp using jsp tags

开发者 https://www.devze.com 2022-12-19 11:47 出处:网络
Class Foo { public String currentVersion() { return \"1.2\"; } } need to call currentVersion class method from jsp using tag library? where currentversion is not getter or setter method ,it is 开发者
Class Foo
{
   public String currentVersion()
   {
     return "1.2";
   }
}

need to call currentVersion class method from jsp using tag library? where currentversion is not getter or setter method ,it is 开发者_如何学Gojust a class method.


You can't do this with standard JSP expressions like ${foo.currentVersion}, that only works with bean proprties (i.e. getCurrentVersion()).

You need to either

  • Write a scriptlet (don't do it!)
  • Write a custom tag class that calls currentVersion() for you
  • Refactor Foo to have a getCurrentVersion() method


Simply rename you method to getCurrentVersion(), there is nothing in the JavaBeans specification that says that a "getter" method has to return the value of an attribute on the class. A getter method can simply return a constant value like you have.

class Foo {
    public String getCurrentVersion() {
        return "1.2";
   }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号