How to get boolean property with expression language?

醉酒当歌 提交于 2020-01-03 19:34:33

问题


If I have a class like this:

class Person {
  private int age;
  public int getAge() {
    return age;
  }
  public boolean isAdult() {
    return age > 19;
  }
}

I can get the age with EL like this:

${person.age}

But, I cannot figure out how to get the isAdult(). How can I get this?


回答1:


Do it like

${person.adult}

It will invoke isAdult()

It works on java bean specifications.




回答2:


Doing ${person.adult} should work, unless you are using a very old version of JSP, in which case you may need to change your method name to getAdult() or even getIsAdult().

Essentially this same question was asked (and answered) here: getting boolean properties from objects in jsp el




回答3:


The JavaBean specification defines isXXX for boolean getters and getXXX for other getter, so it should be exactly the same syntax: ${person.adult}.




回答4:


try this

 class Person {
  private int age;
  private boolean adult;
  public int getAge() {
    return age;
  }
  public void isAdult() {
    adult = (age > 19);
  }
}

${person.adult}


来源:https://stackoverflow.com/questions/6854866/how-to-get-boolean-property-with-expression-language

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