I'm not aware of any easy way to do this with JSF1.1.
The EL language is very simple; it's not a full programming language
that allows sub-expressions etc. It's really just a "path" to the
desired value. The expression
foo['bar']
is equivalent to
foo.get('bar')
As a minor bit of syntactic sugar, if the bit within the square braces
is a single "word" then the quotes can be omitted, eg foo[bar]. However
unlike normal programming languages the bit within the square braces
cannot be an expression. I don't know the new EL expression for JSP2.1
but am not aware of any change in this area.
In addition, EL expressions work only on java bean properties. And by
default statics are not bean properties. This can be worked around by
defining a BeanInfo class that controls what "properties" are available
on an object, so it then becomes possible to access constants like
<t:outputText value="#{bean.SOME_CONST}"/>
However this does not solve your problem: that EL does not support
"sub-expressions".
There are a couple of reasons for keeping the EL simple:
(a) it would not be nice for each JSF implementation to have to include
a parser for a complete language!
(b) logic should really be in the backing beans not the JSP page.
Limiting EL expressions to simply specifying *what* is to be displayed
(rather than how it is to be computed) helps keep logic out of the jsp
pages.
In the spirit of (b) above, you might want to rethink whether the JSP
page should be assuming the presence of a map that can be indexed into
via constants on a specific class, ie this issue might be indicating the
code does not have a clean separation between presentation and logic.
However if you really want to do this from the JSP then perhaps on your
bean class you could define:
public Map getVariablesMap() {
return new IndexedByConst(realVarMap, ProcessConstants.class);
}
where the IndexedByConst class subclasses AbstractMap and defines the
get as follows:
public Object get(Object constName) {
// use reflection to find the constant field on its target class
// (ProcessConstants in this example) which has name constName
String key = ....;
// now fetch the desired value from the 'real' map:
return realVarMap.get(key);
}
then your page can contain:
<h:outputText value="#{bean.variablesMap[CC_MESSAGES]}"/>
You could get even fancier and support this:
<h:outputText
value="#{bean.variablesMap['ProcessConstants.CC_MESSAGES']}"/>
where the custom map's get method splits its parameter into class and
field parts then dynamically loads the class as well as the const.
I hope this helps.
Regards,
Simon
[EMAIL PROTECTED] wrote:
Hi,
I have all the constants in an interface as follows.
public interface ProcessConstants {
public static final String CC_MESSAGES = "customercareMessages";
.........
.........
}
Now I want to pass these constants as a keys to the map in a JSF page
using JSF EL.
I tried with
<h:outputText id="message"
value="#{bean.variablesMap[ProcessConstants.CC_MESSAGES]}"/>.
But it is not working. If I write like,
<h:outputText id="message"
value="#{bean.variablesMap['customercareMessages']}"/>.
Then it is working. But i have to get the keys from 'ProcessConstants'
interface.
If anybody know about this please let me know.
Thanks & Regards,
Basha