Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Need help on TagAdapter

 
Mukhtar Ahmed
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not able to understand how and when to use TagAdapter class. I understand by reading the specs that SImpleTag does not extend Tag so it cannot be a parent of a Classic tag. But then what is the use of TagAdapter?
How do I use it to make a simple tag a parent of a classic tag?
Can I do something like:
<outerSimple:x>
<innerClassic:y/>
</outerSimple:x>
Where outserSimple:x is a simple tag and innerClassic:y is a classic tag?
 
Mike Wang
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes, you are right TagAdapter is used to allow collaboration between classic Tag handlers and SimpleTag handlers.

according to API Doc
Because SimpleTag does not extend Tag, and because Tag.setParent() only accepts a Tag instance, a classic tag handler (one that implements Tag) cannot have a SimpleTag as its parent. To remedy this, a TagAdapter is created to wrap the SimpleTag parent, and the adapter is passed to setParent() instead. A classic Tag Handler can call getAdaptee() to retrieve the encapsulated SimpleTag instance.
so, you can

Then you can get the simpleTag ref in you classic tag instance which enable the collaboration.
Originally posted by Mukhtar Ahmed:
I am not able to understand how and when to use TagAdapter class. I understand by reading the specs that SImpleTag does not extend Tag so it cannot be a parent of a Classic tag. But then what is the use of TagAdapter?
How do I use it to make a simple tag a parent of a classic tag?
Can I do something like:
<outerSimple:x>
<innerClassic:y/>
</outerSimple:x>
Where outserSimple:x is a simple tag and innerClassic:y is a classic tag?
 
Mukhtar Ahmed
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mike, Thanks for the reply.
Let me just make sure I understand it right. The following code:
TagAdapter tagAdapter=(TagAdapter)childTag.getParent();
SimpleTag simpleTag = (SimpleTag)tagAdapter.getAdaptee();
will be inside the classic tag handler class (the innerClassic:y).
So who will create the TagAdapter? The container? or do we have to implement childTag.getParent(); method so that it returns TagAdapter?
TagSupport.getParent() returns a Tag. So does the implementation of TagSupport class makes sure that TagAdpater object is returned if the parent tag is a SimpleTag?
Also, while writing a JSP page, do we have to change anything is the parent tag of a classic tag is a SimpleTag? Or the code that I wrote above willl work without any modification? I.e will the following code work?
<outerSimple:x>
<innerClassic:y/>
</outerSimple:x>
 
Mike Wang
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are right! the code will be inside the classic tag handler class
If you extends TagSupport, it's no need to implements the .getParent() method , TagSupport has done that for you.


It is the responsiblility of container to call the setParent() method of Tag, When the container decide that the parent tag is an instance of SimpleTag it will create a TagAdapter and makes the parent tag(SimpleTag) the adaptee, then pass the adapter (which is an instance of Tag now) as parameter to
setParent() methed of the child Tag (suppose child tag is a classic tag).
so, TagSupport class insure that TagAdpater object is returned if the parent tag is a SimpleTag.
Originally posted by Mukhtar Ahmed:
Hi Mike, Thanks for the reply.
Let me just make sure I understand it right. The following code:
TagAdapter tagAdapter=(TagAdapter)childTag.getParent();
SimpleTag simpleTag = (SimpleTag)tagAdapter.getAdaptee();
will be inside the classic tag handler class (the innerClassic:y).
So who will create the TagAdapter? The container? or do we have to implement childTag.getParent(); method so that it returns TagAdapter?
TagSupport.getParent() returns a Tag. So does the implementation of TagSupport class makes sure that TagAdpater object is returned if the parent tag is a SimpleTag?
Also, while writing a JSP page, do we have to change anything is the parent tag of a classic tag is a SimpleTag? Or the code that I wrote above willl work without any modification? I.e will the following code work?
<outerSimple:x>
<innerClassic:y/>
</outerSimple:x>
 
Mukhtar Ahmed
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Mike. That clears my doubts. I really appreciate your help.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic