File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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
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: 2057
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: 2057
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: 61221
    
  66

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: 2057
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: 61221
    
  66

There is an adapter class, TagAdapter, for such cases.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to get child attribute in parent tag