my dog learned polymorphism*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes doubt in a mock exam question 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 » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "doubt in a mock exam question" Watch "doubt in a mock exam question" New topic
Author

doubt in a mock exam question

pradeep singh
Ranch Hand

Joined: Oct 23, 2007
Posts: 339
Question 5. Consider a JSP page that contains the following code:

10. <x uter>
11. <x:middle>
12. <x.inner></x.inner>
13. </x:middle>
14. <x uter>

Assuming the following implementation details, identify the correct statements.

1. x uter is implemented by OuterTag.java, which extends SimpleTagSupport.

2. x:middle is implemented by MiddleTag.java, which extends BodyTagSupport and has a body-content of tagdependent.

3. x:inner is implemented by InnerTag.java, which extends BodyTagSupport and has a body-content of tagdependent.

OPTIONS are:
1.OuterTag can get a reference to InnerTag by calling TagSupport.findChildrenWithClass() method.
2.MiddleTag can get a reference to OuterTag by calling TagSupport.findAncestorWithClass(this, OuterTag.class).
3.InnerTag can get a reference to OuterTag by calling TagSupport.findAncestorWithClass(this, OuterTag.class) method.
4.MiddleTag can get a reference to OuterTag by calling TagSupport.getParent() method.
5.InnerTag can get a reference to MiddleTag by calling TagSupport.getParent() method.

Answer is given 4.But I want to know why 3 is wrongsource:enthuware


SCJP 5.0(75%), SCWCD 5.0(88%)
Musab Al-Rawi
Ranch Hand

Joined: Aug 06, 2007
Posts: 231
MiddleTag has a body-content of tagdependent (text) this means that the x:inner will not be invoked since it is TEXT.


SCBCD - SCWCD - SCJD - SCJP - OCA
pradeep singh
Ranch Hand

Joined: Oct 23, 2007
Posts: 339
Please anybody tell me why option 2 is wrong .
Shashank vanjare
Greenhorn

Joined: Apr 10, 2008
Posts: 9
The method findAncestorWithClass is defined in the class
SimpleTagSupport, since the inner tag has classic tag implemenation it
cannot call that method
pradeep singh
Ranch Hand

Joined: Oct 23, 2007
Posts: 339
The method findAncestorWithClass is defined in the class
SimpleTagSupport, since the inner tag has classic tag implemenation it
cannot call that method



This method is also defined in TagSupport class.So why option 2 is wrong.
pradeep singh
Ranch Hand

Joined: Oct 23, 2007
Posts: 339
Any update?
Dee Brown
Ranch Hand

Joined: Jun 14, 2008
Posts: 94
This is one possibility:

For the findAncestorWithClass methods, in TagSuport the return type is Tag, while in SimpleTagSupport the return type is JspTag. Since OuterTag.class (SimpleTagSupport) is not in the inheritance hierarchy of classes that implement the Tag interface, it is not possible the Tag reference that is returned by TagSuport.findAncestorWithClass to have any association with OuterTag class (because they are not in the same inheritance hierarchy).

The Api (v2.1, p1-151) states, "A findAncestorWithClass method is available on SimpleTagSupport.This should be used,instead of
TagSupport.findAncestorWithClass(), in all cases where the desired return value may implement SimpleTag." It goes on to discuss TagAdapter usage, which is a way around the above restriction.
John Pearson
Ranch Hand

Joined: Oct 19, 2007
Posts: 51
Dee's right. A classic tag handler doesn't know anything about SimpleTags. It can only take a Tag for setParent(), and so that what it returns for getParent(), which is what's called by findAncestorWithClass(). To make it possible for a classic tag to have a SimpleTag parent, the SimpleTag gets wrapped by a TagAdapter, which does implement the Tag interface.

If getParent() returns a TagAdapter, you can then call TagAdapter's getAdaptee() method to get the SimpleTag that you really wanted.

So option 2 won't work. If you really want to use findAncestorWithClass(), you can call findAncestorWithClass(this, TagAdapter.class), followed by getAdaptee(). If you have more than one SimpleTag in the hierarchy, you'll of course need to check that the SimpleTag returned by getAdaptee() is, in fact, an instance of the SimpleTag class you're looking for.

By the way, this is way more detail than you need for the exam, but I always liked to know how things really worked.
 
 
subject: doubt in a mock exam question
 
Similar Threads
custom tag doubt!
Classic Tags > tagdependent
BodyTagSupport + EVAL_BODY_BUFFERED + tag hierarchy
A tag can call its parent
classic tag access a simple parent?