如何Java的前pression语言解决布尔属性? (在JSF 1.2) [英] How does Java expression language resolve boolean attributes? (in JSF 1.2)
问题描述
所以,我们都知道,#{someBean.value}
将尝试并获得 someBean
名为值
。它会寻找的getValue()
。但是,如果这个属性是布尔
?它会寻找则isValue()
。什么也不会寻找的是 hasValue的()
。
这让我思考,究竟是什么呢?
Java EE 5教程章 - 统一前pression语言指使用pageContext.findAttri-bute()
。 的PageContext
送你的JspContext
。他们没有真正解释它们下面,以确定他们正在寻找方法的名称规则。
这也很容易找到的文档,上面写着该方法名必须以get开始。但是,我知道,则isValue()
的作品。
任何人都可以点我到这是写下来的文档。我不是在找我找参考教程或例子。
它的权威在这两个的 JavaBeans规范和的 EL规范。
要采取布尔
属性为例,它的JavaBeans 8.3.2章节的描述规格:
8.3.2布尔属性
此外,对于布尔属性,我们允许一个getter方法匹配的模式:
公共布尔为<属性名>()
;
这为< 属性名>可以提供替代的方法获得<的属性名>的方法,
或者它可以除了一个来提供。获得&下; <青霉>的PropertyName 青霉>>方法
在这两种情况下,如果为&lt; 属性名>的方法是一个布尔属性present那么我们将
使用为&lt; 属性名>方法来读取属性值
一个例子布尔属性可能是:
公共布尔isMarsupial();
公共无效setMarsupial(布尔米);
块引用>因此,
#{bean.marsupial}
期望的是上面的getter / setter对。和在EL规范的1.18.5章节:
1.18.5强制转换一个布尔
- 如果
A
是空
或,
,返回假
- 否则,如果
A
是布尔
,返回A
- 否则,如果
A
是字符串
和通过Boolean.valueOf(A)
不抛出
例外,返回它
块引用>参见:
So we all know that
#{someBean.value}
will try and get the content of some property onsomeBean
calledvalue
. It will look forgetValue()
. However, what if this property isboolean
? It will look forisValue()
. What it won't look for ishasValue()
.This got me thinking, what exactly does it do?
Java EE 5 tutorial chapter - Unified Expression Language refers to
PageContext.FindAttribute()
.PageContext
sends you toJSPContext
. None of them actually explain the rules they are following to determine the name of the method they are looking for.It is also fairly easy to find documentation that says the method names must begin with get. However, I know that
isValue()
works.Can anyone point me to documentation where this is written down. I'm not looking for tutorials or examples I'm looking for reference.
解决方案It's authoritatively documented in both the JavaBeans Spec and EL Specification.
To take the
boolean
property as an example, it's described in chapter 8.3.2 of JavaBeans spec: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);
So,
#{bean.marsupial}
expects exactly the above getter/setter pair.And in chapter 1.18.5 of EL spec:
1.18.5 Coerce A to Boolean
- 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
See also:
- javax.el.PropertyNotFoundException: Property 'foo' not readable on type java.lang.Boolean
- Java: how to name boolean properties
这篇关于如何Java的前pression语言解决布尔属性? (在JSF 1.2)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!