*
The moose likes JSP and the fly likes How to invoking a (dynamic) property on a (dynamic) bean? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "How to invoking a (dynamic) property on a (dynamic) bean?" Watch "How to invoking a (dynamic) property on a (dynamic) bean?" New topic
Author

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

Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5147
    
  12

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)?

    SCJA, SCJP (1.4 | 5.0 | 6.0), SCJD
    http://www.javaroe.be/
    Bear Bibeault
    Author and ninkuma
    Marshal

    Joined: Jan 10, 2002
    Posts: 60816
        
      65

    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.


    [Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
    Roel De Nijs
    Bartender

    Joined: Jul 19, 2004
    Posts: 5147
        
      12

    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

    Joined: Jan 10, 2002
    Posts: 60816
        
      65

    If you want to do it in the EL, that's easy: ${bean[property]}
    Roel De Nijs
    Bartender

    Joined: Jul 19, 2004
    Posts: 5147
        
      12

    Works like a charm! Thanks!

    Such a simple and easy solution definitely deserves a cow.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: How to invoking a (dynamic) property on a (dynamic) bean?
     
    Similar Threads
    ApplicationContext vs WebApplicationContext and ServletContext
    Dynamic Textfield In JSF 2.0
    Dynamically change the class according to the variables
    using ajax and struts iterate tags
    dynamically rerender datatable content from managedbean?