如何Java的前pression语言解决布尔属性? (在JSF 1.2) [英] How does Java expression language resolve boolean attributes? (in JSF 1.2)

查看:222
本文介绍了如何Java的前pression语言解决布尔属性? (在JSF 1.2)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我们都知道,#{someBean.value} 将尝试并获得 someBean 名为。它会寻找的getValue()。但是,如果这个属性是布尔?它会寻找则isValue()。什么也不会寻找的是 hasVa​​lue的()

这让我思考,究竟是什么呢?

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 on someBean called value. It will look for getValue(). However, what if this property is boolean? It will look for isValue(). What it won't look for is hasValue().

This got me thinking, what exactly does it do?

Java EE 5 tutorial chapter - Unified Expression Language refers to PageContext.FindAttribute(). PageContext sends you to JSPContext. 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 is null or "", return false
  • Otherwise, if A is a Boolean, return A
  • Otherwise, if A is a String, and Boolean.valueOf(A) does not throw an exception, return it

See also:

这篇关于如何Java的前pression语言解决布尔属性? (在JSF 1.2)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆