Wednesday, 18 September 2013

taglib directive

(3.) taglib directive

This is the last and most important directive
Basially we provide certain tags in your jsp pages and implementing the taglib directive is going to help a lot because it is going to clean up your jsp page, it is going to look more pretty it is going to get a lot of loat of your shoulder as well.
So lot of common custom logic which you have in your application can be written in a taglib directive and you simply use the taglib directive in your jsp pages instead of again writing all those logic repeatedly in each of the page .

syntax: <%@ taglib uri="{TLD_FILE}" prefix="{PREFIX}"%>

It starts angular brackets percentage atTheRate and followed by taglib and we have uri and prefix, we will see what is really mean?
Components of taglib Libraries:

1. Tag handler class (2) Descriptor configuration file (3) taglib directive

1. Tag handler class
Now let's say that we have a common functionality used in each and every page in my application so what I do this, I put this common chunk of code in certain file and I will use this common code in multiple jsp pages.
So that common place where I'm puttin the common logic is the Tag Handler class. This is the place where I put the common custom logic and html generation.
And I use the taglib directive in my jsp page to invoke the class which is implementing the common logic now they have to be some bridge between the taglib directive and my jsp file and actual tag handler class so for that reason I have the Descriptor configuration file.
Descriptor configuration file is nothing but the property mapping file which has got information about the taglib directive and the tag handler class and what are the different property you can set for the tag libraries

overview of taglib directive:
jsp4.jpg
//test.jsp
<%@taglib uri="sharmanjTags" prefix ="sharmanj"%>
<sharmanj:txtbox length="10" name="userName"/>
....

//sharmanj-taglib.tld
<tag>
<name>txtBox</name>
<tagclass>TextBoxTag</tagclass>
<bodycontent>EMPTY</bodycontent>
<attribute>
<name>name</name>
<required>true</required>
</attribute>
....
</tag>


//web.xml
<web-app>
<taglib>
<taglib-uri>sharmanTags<taglib-uri>
<taglib-location>
sharmanj-taglib.tld
</taglib-location>
</taglib>
</web-app>

/*TxtBoxTag.java*/
public class TxtBoxTag extends TagSupport
{
   public int doStartTag()
   {
     ----
   }
 }

See here an example i will show all the different pages  classes and files involved in a tag library and how each file is connected to the other file

//test.jsp
<%@taglib uri="sharmanjTags" prefix ="sharmanj"%>
......
<sharmanj:txtbox length="10" name="userName"/>
....

So first thing is my test.jsp, here What I did is? I have a common logic for displaying the text box so in my application whenever someone wants to use a text box in a jsp page they should not use the predefined text box present in html whatever input. But they have to use the jsp tag called sharmanj:txtbox to add a text box in their html or jsp file.
Lets say that I have provided some logic for that and that logic is present in the TxtBoxTag.java
We will have a look at the whole life cycle of tag handlers and all those things, what are the different tag handlers we can have? and whole life cycle of tag handlers and the next future class we will discuss tag libraries more details but here I just wanted to show you the different components we have placed tag libraries and how communication takes place?  between these components.
So my custom logic for the text box representation goes in the TxtBoxTag which is nothing but the tag handler class ( we just discussed).

/*TxtBoxTag.java*/
public class TxtBoxTag extends TagSupport
{
 public int doStartTag()
 {
  ----
 }
}

Lets say that I have that logic is placed in doStartTag() method
Now in my jsp I need to use the logic present in the TxtBoxTag.java to represent my text box.

//test.jsp
<%@taglib uri="sharmanjTags" prefix ="sharmanj"%>
......
<sharmanj:txtbox length="10" name="userName"/>
....

So here I say I get the taglib directive and I say:" uri=sharmanjTags and prefix=sharmanj. When I say uri? it means that path to the tag libraries which I will be using and this path will be present in the web.xml, and which is the configuration for your servlets and jsps
web.xml is the configuration file for your servlet and jsps

//web.xml
<web-app>
<taglib>
<taglib-uri>sharmanTags<taglib-uri>
<taglib-location>
sharmanj-taglib.tld
</taglib-location>
</taglib>
</web-app>
So when I say taglib uri="sharmanj", What the container is going to do is? The Web Container finds the sharmanTags in the web.xml file under the <taglib> element
So web container is going to find the taglib uri with the name "sharmanjTags", so it finds a match now.
Once the web container finds the match in web.xml file
Now web container is going to see the <taglib-location>, so taglib-location is "sharmanj-taglib.tld"
Now web container goes to sharmanj-taglib.tld location which is a configuration file again where we provide certain different configurations for the tag libraries

//sharmanj-taglib.tld
<tag>
<name>txtBox</name>
<tagclass>TxtBoxTag</tagclass>
<bodycontent>EMPTY</bodycontent>
<attribute>
<name>name</name>
<required>true</required>
</attribute>
....
</tag>
And within the sharmanj-taglibraries , I have different tag attributes I can use in this particular tablibraries i.e sharmanj-taglib.tld
And before that let me complete the text.jsp

//test.jsp
<%@taglib uri="sharmanjTags" prefix ="sharmanj"%>
......
<sharmanj:txtbox length="10" name="userName"/>
....
I said that Im using the sharmanjTags lib and prefix can be anything example: sharmanj
So whatever I put prefix I should use that prefix certain feature in the taglib that is when I say prefix "sharmanj", Iam using prefix sharmanj on every element where I want to use the taglib. That is when I say sharmanj:txtbox, so here Iam trying to use  txtbox.
We know that taglib directive match to this particular location (i.e sharmanj-taglib.tld)  now Im going to use the "txtbox" taglib. Now it is going to search "txtbox" in the <name> tag in the file sharmanj-taglib.tld
Going to see what is the corresponding <tagclass> and it sees that tag class is "TxtBoxTag " and it is going to find that "TxtBoxTag" class , implement the basically call the methods in the TxtBoxTag.java

/*TxtBoxTag.java*/
public class TxtBoxTag extends TagSupport
{
 public int doStartTag()
 {
  ----
 }
}
So I put all my custom logic difining the TxtBox in my page.
So this is how the flow navigates in the different components and how your tag handler class is identified by the jsp pages .

Now finished the jsp taglibs

No comments:

Post a Comment