why inject the static part in the EL-expression
just write the custom resolver and have him check whether the property
name resolves to a static-classmember and return it...
 
hth
Alexander

________________________________

From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Craig
McClanahan
Sent: Wednesday, February 07, 2007 10:52 PM
To: MyFaces Discussion
Subject: Re: AW: Accessing constants in EL expressions




On 2/7/07, Simon Kitching <[EMAIL PROTECTED]> wrote: 

        The EL language is defined to provide access to Java Bean
Properties,
        and only Java Bean Properties. Static fields are not regarded as
Java
        Bean Properties by the java.beans.Introspector class, hence are
not
        accessable. 
        
        Yes, it sucks but this is the JSF spec. Well, actually the JSF
spec says:
        <quote>
        The syntax of a value binding expression is identical to the
syntax of
        an expression language expression defined in the JavaServer
Pages 
        Specification (version 2.0), sections 2.3 through 2.9, with the
        following exceptions ...
        </quote>
        where the exceptions are not relevant in this case. And the JSP
spec
        says that a.b and a[b] are:
        <quote> 
        used to access maps, lists, arrays of objects and properties of
a Java-
        Beans object
        </quote>
        
        See:
            http://java.sun.com/javaee/javaserverfaces/download.html 
            http://java.sun.com/products/jsp/reference/api/index.html
        
http://java.sun.com/products/jsp/syntax/2.0/syntaxref207.html#1010522
<http://java.sun.com/products/jsp/syntax/2.0/syntaxref207.html#1010522> 
        
        Therefore to get access to static constants working, what is
needed is
        to ensure that the data you want *is* a Java Bean Property
(read-only in 
        the case of constants). The ways to do this are:
        1. add a getter method, getCONSTANT
        2. add a getter method for a Map which provides access to the
constants
        3. play clever tricks with a BeanInfo class.


If you want to solve it once and for all, consider implementing a custom
PropertyResolver to do the dirty work.  The goal would be to translate
something like

    #{foo.static.BAR}

into the value of the static constant BAR on whatever class foo resolves
to.  The getValue() method for this custom PropertyResolver would need
to do something like this: 

* If the property name is "static", create some sort of proxy object
  that is configured with the class of the base object and return that.

* Else if the base object is an instanceof the proxy class described
above, 
  treat the property name as the name of a static constant, use
reflection
  to retrieve the value, and return that.

* Else delegate to the previous PropertyResolver instance.

Being able to customize the evaluation of expressions is very powerful. 

Also, while the concept above refers to PropertyResolver and is
therefore relevant for JSF 1.1, the same concept works with ELResolver
in JSF 1.2 (with the added benefit that your custom resolvers are used
for JSP ${...} expressions as well as JSF #{...} expressions). 

Craig


Reply via email to