GeeCON Prague 2014*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes could not understand tagdir attribute of taglib directive ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "could not understand tagdir attribute of taglib directive ?" Watch "could not understand tagdir attribute of taglib directive ?" New topic
Author

could not understand tagdir attribute of taglib directive ?

gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 924
    
    1

the hfsj book says that

1. the container searches for tag files in WEB-INF/tags or subdirectory or META-INF/tags or subdirectory(inside jar file in WEB-INF/lib).

2. if the tag files is deployed in jar file we need to specify that in tld like <tag-file><name>Header</name><path>/META-INF/tags/Header.tag</path></tag-file>

suppose my tag file is in WEB-INF/tags/one/two/three/Header.tag

now container is supposed to find this tag file automatically since it is container specified location. right. if this is the case then WHY we need to specify the full path to tag file in taglib directive like this:

<%@taglib prefix="myadvice" tagdir="/WEB-INF/tags/one/two/three" %>

cant container prepare a map or something like it do with tld files ?
Paul Anilprem
Enthuware Software Support
Ranch Hand

Joined: Sep 23, 2000
Posts: 3305
    
    7
It can. tagdir just tells the container that you are going to use the prefix specified by the prefix attribute for all the tags installed in the directory given by tagdir attribute in that JSP page.

It is not like you absolutely have to use tagdir attribute. If you know the uri value (you can look it up in the tld file), you can use the uri attribute instead of tagdir.


Enthuware - Best Mock Exams and Questions for Oracle/Sun Java Certifications
Quality Guaranteed - Pass or Full Refund!
Frits Walraven
Creator of Enthuware JWS+ V6
Bartender

Joined: Apr 07, 2010
Posts: 1686
    
  25

Yes and it also makes it quite flexible because I can now have 2 tags with the same name (e.g. mytag.tag) but attach it to another prefix:



Regards,
Frits
gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 924
    
    1

Thanks Paul and Frits. i understand that we can use uri also in place of tagdir. also fritz i got your point that prefixes act as namespaces and we can use same name for our tags/tagfile. but still i have a doubt.

if i use uri in place of tagdir then in the TLD file i will have something like this. assume that tag file is deployed in jar file at /META-INF/tags


<uri>/myuri</uri>
<tag-file>
<name>Header</name>
<path>/META-INF/tags/Header.tag </path>


</tag-file>

now suppose in jsp i use the following

<%@taglib prefix="myadvice" uri="/myuri" %>

<myadvice:Header />


now when the jsp engine encounters the tag file tag then it sees that i'm using prefix myadvice which is a namespace for uri /myuri. the container already knows where the uri points to so when it "reads" information from the .tld file it sees the tag-file tag containing name Header and the corresponding path to .tag file. using the path it invokes the tag file. first of all am i right ?

if am right then that means the container found out the path to tag file from the .tld file. the what is the whole point about container automatically locating .tag files in /WEB-INF/tags or subdirectory or META-INF/tags or subdirectory .
Frits Walraven
Creator of Enthuware JWS+ V6
Bartender

Joined: Apr 07, 2010
Posts: 1686
    
  25

now when the jsp engine encounters the tag file tag then it sees that i'm using prefix myadvice which is a namespace for uri /myuri. the container already knows where the uri points to so when it "reads" information from the .tld file it sees the tag-file tag containing name Header and the corresponding path to .tag file. using the path it invokes the tag file. first of all am i right ?

Yes, more or less. The calls to the Tag-file functionality will be included in the Java class that is made from the JSP (just have a look at the generated file). As far as I remember the container will generate a SimpleTagSupport class from every Tag-file. The calls to the generated SimpleTagSupport are then included in the generated Servlet.

if am right then that means the container found out the path to tag file from the .tld file. the what is the whole point about container automatically locating .tag files in /WEB-INF/tags or subdirectory or META-INF/tags or subdirectory

I am not sure whether I understand your question, but there are two options:
  • 1) Tag File is located in /WEB-INF/tags/ or one of its subdirectories
  • 2) Tag File is located in /WEB-INF/lib/ in a Jar file and then inside the Jar file in a directory /META-INF/tags or one of its subdirectories

  • An URI can only be used if your are trying to access a Tag-file that is located in the Jar-file. The TLD will then tell the container where to find the Tag-file.
    If you are using a Tag file located in /WEB-INF/tags (and the tagdir attribute in the JSP) the container will generate a TLD that will point to the Tag file, making the two possibilities almost equal.

    Regards,
    Frits
     
     
    subject: could not understand tagdir attribute of taglib directive ?