所以我们都知道#{someBean.value}
将尝试获取 someBean
上某些属性的内容调用value
.它将寻找 getValue()
.但是,如果这个属性是 boolean
怎么办? ?它将寻找 isValue()
.它不会寻找的是hasValue()
.
这让我思考,它到底是做什么的?
Java EE 5 tutorial chapter - Unified Expression Language指PageContext.FindAttribute()
. PageContext
将您发送至 JSPContext
.他们都没有真正解释他们遵循的规则来确定他们正在寻找的方法的名称。
也很容易找到说明方法名称必须以 get 开头的文档。但是,我知道 isValue()
作品。
任何人都可以指出我写下来的文档。我不是在寻找我正在寻找引用的教程或示例。
最佳答案
它在 JavaBeans Spec 中都有权威记录。和 EL Specification.
拍boolean
以属性为例,它在 JavaBeans 规范的第 8.3.2 章中有描述:
8.3.2 Boolean properties
In addition, for boolean properties, we allow a getter method to match the pattern:
public boolean is<PropertyName>()
;This “is<PropertyName>” method may be provided instead of a “get<PropertyName>” method, or it may be provided in addition to a “get<PropertyName>” method.
In either case, if the “is<PropertyName>” method is present for a boolean property then we will use the “is<PropertyName>” method to read the property value.
An example boolean property might be:
public boolean isMarsupial(); public void setMarsupial(boolean m);
所以,
#{bean.marsupial}
完全期望上面的 getter/setter 对。在 EL 规范的第 1.23.5 章中:
1.23.5 Coerce A to Boolean or boolean
- If
A
isnull
and the target type is not the primitive typeboolean
, returnnull
- If
A
isnull
or""
, returnfalse
- Otherwise, if
A
is aBoolean
, returnA
- Otherwise, if
A
is aString
, andBoolean.valueOf(A)
does not throw an exception, return it- Otherwise, error
也可以看看:
关于jsp - Java 表达式语言如何解析 boolean 属性? (在 JSF 1.2 中),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3236929/