• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

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: 67752
    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: 67752
    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.
     
    Normally trees don't drive trucks. Does this tiny ad have a license?
    Gift giving made easy with the permaculture playing cards
    https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
    reply
      Bookmark Topic Watch Topic
    • New Topic