• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

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

 
Sheriff
Posts: 11604
178
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)?
     
    Sheriff
    Posts: 67260
    170
    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: 11604
    178
    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
    Sheriff
    Posts: 67260
    170
    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: 11604
    178
    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.
     
    For my next feat, I will require a volunteer from the audience! Perhaps this tiny ad?
    how do I do my own kindle-like thing - without amazon
    https://coderanch.com/t/711421/engineering/kindle-amazon
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!