• Post Reply Bookmark Topic Watch Topic
  • New Topic

EL nested usage

 
Daoyue Ming
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
<hutputText 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 <hutputText value='#{beanA.beanX.propX}'/>, as beanX.propX is not a static string. I tried <hutputText value='#{beanA[beanX.propX]}'/> as well, it can't automatically get the second level from beanX.

anyone can help me?
[ July 06, 2006: Message edited by: Daoyue Ming ]
 
Tim Holloway
Bartender
Posts: 18417
60
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'll let you in on a secret. All this stuff is handled by the Apache beanutils, which is a general-purpose library that allows you to do an extended set of the kinds of functions that Java's introspection mechanism supports. It also supports accessing data via expressions such as the ones you've listed. You can find details about beanutils at http://jakarta.apache.org/commons/beanutils/ .

But as a quick guide, reading a value from "beanA.beanX.propX" is the same thing as the java code "beanA.getBeanX().getPropX()".

If I haven't confused myself, I think that your use of square brackets in the expression '#{beanA[beanX.propX]}' is wrong though - that's for array indexing. So what you probably want is more like "#{beanA(beanX).propX}". Note that this means "beanA.get(beanX).getPropX()", where "propX" is a property of the value object returned from a collection where "beanX" is a key object. As opposed to ""#{beanA(beanX.propX)}" which is "beanA.get(beanX.getPropX())".
[ July 06, 2006: Message edited by: Tim Holloway ]
 
Daoyue Ming
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can use #{beanA[beanX.propX]} if propX is primitive type, but if it is another class, it doesn't work.

I have tried your solution, doesn't work either
 
Tim Holloway
Bartender
Posts: 18417
60
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, the outcome of your expression must ultimately become a String or a primitive - you can't use the EL to return a compound object.

If you want to enumerate properties, you'll have to set things up so that the subproperties are each presented as simple objects. In JSF this may be a pain, since you can't embed looping constructs in JSF, only wrap collections and use the table components.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!