4

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?

Sanghyun Lee
  • 19,746
  • 18
  • 92
  • 122

4 Answers4

6

Do it like

${person.adult}

It will invoke isAdult()

It works on java bean specifications.

jmj
  • 232,312
  • 42
  • 391
  • 431
  • Thanks! Is there any other getters? Can I get property from the method like `hasSomething()`? – Sanghyun Lee Jul 28 '11 at 06:38
  • No standard accessor doesn't have this pattern. this is generally convention to name a method , please see the java beans specs – jmj Jul 28 '11 at 06:49
1

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

Community
  • 1
  • 1
aroth
  • 52,868
  • 20
  • 134
  • 172
0

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

Kilian Foth
  • 13,440
  • 4
  • 38
  • 55
0

try this

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

${person.adult}
Abdul
  • 555
  • 1
  • 5
  • 21