How does Java expression language resolve boolean attributes? (in JSF 1.2)

前端 未结 2 1692
天命终不由人
天命终不由人 2020-11-29 10:29

So we all know that #{someBean.value} will try and get the content of some property on someBean called value. It will look for g

相关标签:
2条回答
  • 2020-11-29 10:48

    Basically what you've stated is all there is to it. EL expects the object to follow regular java bean standards. These 2 should help:

    • http://docstore.mik.ua/orelly/java-ent/jnut/ch06_02.htm
    • http://docs.oracle.com/javaee/1.4/tutorial/doc/JSPIntro7.html#wp71019
    0 讨论(0)
  • 2020-11-29 11:08

    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:

    • javax.el.PropertyNotFoundException: Property 'foo' not readable on type java.lang.Boolean
    • Java: how to name boolean properties
    0 讨论(0)
提交回复
热议问题