This week's book giveaway is in the Performance forum.
We're giving away four copies of The Java Performance Companion and have Charlie Hunt, Monica Beckwith, Poonam Parhar, & Bengt Rutisson on-line!
See this thread for details.
Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to get child attribute in parent tag

 
Saurabh Saha
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.
 
Jesus Angeles
Ranch Hand
Posts: 2068
  • 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: 2068
  • 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.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64975
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • 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: 2068
  • 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
Author and ninkuma
Marshal
Pie
Posts: 64975
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is an adapter class, TagAdapter, for such cases.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic