Thursday, August 15, 2013

How to create custom tag ?

When we need to write custom tag?

Generally we should not write scriptlets in jsp to do some logic.Expression tags,Standard actions and
jstl may not be sufficient to meet out specific tags.

Eg:
If I want to retrieve records from a table and show it on the jsp with out Scriptlets..there is no other
way other than implementing your own custom tag.

<mytags:displayFriends/>


Is it difficult to write custom tag ?

It looks difficult but if we understand the process it is not that difficult.

Steps to write custom tags ?

We are going to develop simple custom tag.

<mytags:helloWorld>

Whenever we place <mytags:helloWorld> in jsp we need to display
"Welcome to JSTL, it is very easy" message in jsp.

1)We need to write a class that extends SimpleTagSupport.
   SimpleTagSupport implements SimpleTag interface

   We will develop HelloWorld tag for our example.HelloWorld need
    to extend SimpleTagSupport.
    
2)We need to override doTag method with our logic to display "Welcome to JSTL,it is very easy".

   Example code:
  
   public void doTag() throws JspException, IOException {
      
        getJspContext().getOut().print("Welcome to JSTL,it is very easy");
       
       
    }
   
 
3)We need to create tld file with following contents.Tld should be located in WEB-INF
   folder.I am placing it in WEB-INF/tlds/hello.tld.

  Contents of TLD file:

 <?xml version="1.0" encoding="UTF-8"?>
<taglib version="2.1" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd">
  <tlib-version>1.0</tlib-version>
  <short-name>hello</short-name>
  <uri>/WEB-INF/tlds/hello</uri>
  <tag>
      <name>helloWorld</name>
      <tag-class>customtags.HelloWorld</tag-class>
      <body-content>empty</body-content>
  </tag>
</taglib>


What is the purpose of TLD ?

Whenever container encounters <mytags:helloWorld/>,it searches tld file with name helloWorld
and then calls the doTag method in the class declared by <tag-class> element.In above declaration
we have specified customtags.HelloWorld.So doTag method in HelloWorld gets invoked by
container whenever it sees <mytags:helloWorld/>

Write a test jsp to test our custom tag


<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="mytags" uri="/WEB-INF/tlds/hello" %>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <mytags:helloWorld/>
    </body>
</html>
 


We have to use taglib directive in jsp page to specify the prefix and uri.

What is URI in taglib directive ?

The value need to be matched with the <uri> element's value specified in TLD file.
In our TLD file we have given uri as <uri>/WEB-INF/tlds/hello</uri>.So in jsp
page directive give uri value as  uri="/WEB-INF/tlds/hello".


What is prefix ?

prefix is just a dummy name,you can give whatever you want.
If you give prefix as mytags

Then in jsp you need to give <mytags:helloWorld>

If you give prefix as hellotags

Then in jsp you need to give <hellotags:helloWorld>

What is JspContext?

JspContext is set by the container while creating the custom tag object.
It automatically happens and set by the jspContainer.JspContext is superclass
for pageContext.From JspContext you can access JspWriter to output some
content to the browser.

In doTag, we will first getJspContext and from JSPContext object we will get
out(JspWriter object) and we will call print method that displays the content
on the browser.

getJspContext().getOut().print("Welcome to JSTL,it is very easy");

Uploaded the code to following URL

https://docs.google.com/file/d/0BwZaaDwCofcNSkJINkZEVll3WHM/edit?usp=sharing







 






















No comments:

Post a Comment