JSF EL with nested properties
I have a problem to use EL to display nested properties.
suppose I have a beanA, and beanB; beanA contains beanB, and beanB has property propA.
if I want to display propA, I can use
<h:outputText value='#{beanA.beanB.propA}'/>
however if beanA has other beans, and I want to display those beans dynamically, I will take the "beanB.propA" part as a dynamic variable.
of course I can't use <h:outputText value='#{beanA.beanX.propX}'/>, as beanX.propX is not a static string. I tried <h:outputText value='#{beanA[beanX.propX]}'/> as well, it can't automatically get the second level from beanX.
anyone can help me?
I assume you know that the syntax "beanA.propX" is simply a shorthand for 'beanA["propX"]'? So if you know before hand what properties your beanA has, you can dynamically iterate over them like this:
<c:forEach items="#{'propX,propY,propZ'}" var="propName">
<h:outputText value="#{beanA[propName]}" />
</c:forEach>
If you don't know the type ahead of time or want to find the properties dynamically, you should probably write a function to return the collection of properties from a given key. A totally general solution is impossible (since the EL-Resolvers are pluggable, and don't always have a determinable set of keys), but the most common cases would be as follows:
* if the object is a key, return a set of integers 0 .. bean.size() - 1
* if the object is a map, return the key set (bean.keySet())
* otherwise use one of the 'java.beans.Introspector.getBeanInfo()' methods, and then the getPropertyDescriptors() method on the resulting BeanInfo to get the list of bean properties.
No, I would use the standard JSP taglib mechanism to make static methods available to EL expressions within a page. The JavaEE tutorial at
http://java.sun.com/javaee/5/docs/tutorial/doc/JSPIntro7.html#wp102387
describes how to do this. Are you using JavaEE 5, or an earlier version?