This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes JSP and the fly likes how to get child attribute in parent tag 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 get child attribute in parent tag" Watch "how to get child attribute in parent tag" New topic
Author

how to get child attribute in parent tag

Saurabh Saha
Ranch Hand

Joined: Dec 08, 2004
Posts: 52
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

Joined: Feb 26, 2005
Posts: 2049
are list and list item, jstl tags? or custom tags you created?
Saurabh Saha
Ranch Hand

Joined: Dec 08, 2004
Posts: 52
Hi,
This is a custom tag that I want to create.

Thanks!
Saurabh
Jesus Angeles
Ranch Hand

Joined: Feb 26, 2005
Posts: 2049
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

Joined: Jan 10, 2002
Posts: 60785
    
  65

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Jesus Angeles
Ranch Hand

Joined: Feb 26, 2005
Posts: 2049
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

Joined: Jan 10, 2002
Posts: 60785
    
  65

There is an adapter class, TagAdapter, for such cases.
 
 
subject: how to get child attribute in parent tag
 
Similar Threads
How to communicate between the child and parent tag
Doubt in simple tag and classic tag access
Get parent node dynamically
Replace tag value using Ant Script
java 1.5 list iteration using jstl tag