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?

[681 byte] By [daoyueminga] at [2007-10-3 0:00:55]
# 1
Maybe you must use some Map or Collection, then iterate it.
Rulasa at 2007-7-14 16:48:23 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2
if it's only one level, then i can use map or collection to look up the value.however if it is nested, it's the same effect, the EL can't look up value propX from beanX.
daoyueminga at 2007-7-14 16:48:23 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3

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.

tony_robertsona at 2007-7-14 16:48:23 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4
but how to place the function?need to implement own variable-resolver, then put it inside faces-config.xml file?
daoyueminga at 2007-7-14 16:48:23 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5

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?

tony_robertsona at 2007-7-14 16:48:23 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6
things are comlicated for me, as i am using facelets, and that page is a common component.not sure whether i can insert my custom tag inside
daoyueminga at 2007-7-14 16:48:23 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...