• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to invoking a (dynamic) property on a (dynamic) bean?

 
Roel De Nijs
Sheriff
Posts: 9795
101
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a tag with 2 attributes: bean (could be any object) and property (a string). This tag should produce a span with
  • id of the span = the given property
  • content of the span = the value of the property of the given bean

  • Example of the desired behavior: <tags:myTag bean="${aList}" property="empty" /> should result in <span id="empty">true</span> (if aList is an empty list)

    The 1st part isn't hard: <span id="${property}"></span>

    But how can I do the 2nd part (if it's possible)?
     
    Bear Bibeault
    Author and ninkuma
    Marshal
    Pie
    Posts: 64618
    86
    IntelliJ IDE Java jQuery Mac Mac OS X
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    In other words, in the tag code you want to get alist.getEmpty()? Or alist.getXyz() if property="xyz"?

    If it's completely arbitrary: reflection.
     
    Roel De Nijs
    Sheriff
    Posts: 9795
    101
    AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Yes, that's exactly what I want to become. If EL expressions could be nested, it would be ${${bean}.${property}}

    We are using Spring MVC, so a spring tag could also be a possible solution. But at 1st (and 2nd) glance I don't see any suitable tag for this issue. If reflection is the only way it can be done, I'll change the tag's attributes to id and value. I can live with <tags:myTag id="empty" value="${aList.empty}" />, although I tried to get rid of having to write 2 times "empty" (but not at all cost).
     
    Bear Bibeault
    Author and ninkuma
    Marshal
    Pie
    Posts: 64618
    86
    IntelliJ IDE Java jQuery Mac Mac OS X
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    If you want to do it in the EL, that's easy: ${bean[property]}
     
    Roel De Nijs
    Sheriff
    Posts: 9795
    101
    AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Works like a charm! Thanks!

    Such a simple and easy solution definitely deserves a cow.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic