Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Creating dynamic EL Method Expressions?

 
Björn Wessman
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!

I would like to combine the values of two different page scoped objects to create a method expression in EL.

I have a bean object "bean" which is a java bean with a number of properties (with getters and setters) and then i have a "beanProperty" object which is the name of the property whose getter i want to invoke on the "bean" object.

In the EL expression, I can "hard code" the name of the property like this: <c:out value="${bean.hardCodedNameOfProperty}"/>

But the name of the property i want to invoke on my bean is stored in my "beanProperty" variable, so what I would like to do is something like this:

<c:out value="${bean.${beanProperty}}"/>

But EL can't parse such an expression...

Does anyone know if it is even possible to achieve what I'm trying to do?

BR,

Björn
 
Devaka Cooray
ExamLab Creator
Marshal
Pie
Posts: 4587
302
Chrome Eclipse IDE Google App Engine IntelliJ IDE jQuery Postgres Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can call it as: ${bean.[beanProperty]}
 
Björn Wessman
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Devaka Cooray wrote:You can call it as: ${bean.[beanProperty]}


Hi Devaka!

Thank you for the reply. I had to remove the dot from your example to make it work, like this:

${bean[beanProperty]}

Thanks for pointing me in the right direction, really appreciate it :)
 
Devaka Cooray
ExamLab Creator
Marshal
Pie
Posts: 4587
302
Chrome Eclipse IDE Google App Engine IntelliJ IDE jQuery Postgres Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Björn Wessman wrote:I had to remove the dot from your example

Yep, that was a typo
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic