• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)?
     
    Sheriff
    Posts: 67746
    173
    Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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: 11604
    178
    Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
    Sheriff
    Posts: 67746
    173
    Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    If you want to do it in the EL, that's easy: ${bean[property]}
     
    Roel De Nijs
    Sheriff
    Posts: 11604
    178
    Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Works like a charm! Thanks!

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