如果一个boolean类型JavaBean有两个get方法(boolean默认是is方法):
public boolean getBool() {
return true;
}
public boolean isBool() {
return false;
}
问题:页面使用el表达式(即,${bean.bool}),输出结果是哪一个呢?
答案:是isBool返回的。
false
也就是说,isBoolean()方法优先于getBoolean()方法,如果没有isBoolean()方法,才找getBoolean()方法。
而Boolean则只能使用get和set方法取得属性。
private boolean b;
private Boolean bb;
public boolean isB() {
return b;
}
public void setB(boolean b) {
this.b = b;
}
public Boolean getBb() {
return bb;
}
public void setBb(Boolean bb) {
this.bb = bb;
}
本文探讨了在Java中使用EL表达式时,如何选择boolean类型的getter方法。具体介绍了当存在isBool与getBool两种方法时,EL表达式将优先调用isBool方法的情况,并通过示例代码解释了这一行为。
4719

被折叠的 条评论
为什么被折叠?



