Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • 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
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

how to get child attribute in parent tag

 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Please tell me how can we get child attribute value in parent tage.
my tage is:
<mytag:list type="link" display="image">
<mytag:listItem name="saurabh" />
</mytag:list>

I don't want to use id attribute inside my parent tag.
I need to get child tag attribute value inside by parent handler class.
 
Ranch Hand
Posts: 2107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
are list and list item, jstl tags? or custom tags you created?
 
Saurabh Saha
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
This is a custom tag that I want to create.

Thanks!
Saurabh
 
Jesus Angeles
Ranch Hand
Posts: 2107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If your parent is a SimpleTag (SimpleTag interface), then you may have an issue. Please refer to some books on how to get away with it. But if your parent is a Tag (Tag interface), then you can go with this approach below.

You can use the getParent method in the child tag. This method returns a reference to the parent tag instance.

Here's how it can go:

Inside the child class, once you got the reference to the parent instance using getParent(), you can store any data on the parent instance. For example, add an instance variable in the parent tag, like, myName. So in the child tag, you store the name attribute you have, into the parent's myName instance variable. So there you go, the parent now has the name attribute from the child.

Once the child process is done, the control goes back to the parent. At that point, the parent would know the child's name attribute's value, by looking at its instance variable myName.
 
Marshal
Posts: 67415
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

If your parent is a SimpleTag (SimpleTag interface), then you may have an issue.



Should be no issue. getParent() is available for simple tags.

you store the name attribute you have, into the parent's myName instance variable.



I'd not allow direct access, but define a method in the same manner as a bean.
 
Jesus Angeles
Ranch Hand
Posts: 2107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is an issue, only if the parent is a SimpleTag, and the child is a Tag. In all other combinations, it is ok. This case might be doable but needs an extra trick.

This is because the Tag getParent returns a Tag(the SimpleTag getParent returns a JspTag), and the SimpleTag doesnt inherit from Tag.

JspTag
|````|
|````|
|````|
Tag``SimpleTag
[ October 01, 2005: Message edited by: Jesus Angeles ]
 
Bear Bibeault
Marshal
Posts: 67415
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is an adapter class, TagAdapter, for such cases.
 
Consider Paul's rocket mass heater.
    Bookmark Topic Watch Topic
  • New Topic