Thursday, 6 December 2012

JSP custom tag with body

 Custom tag JSP

Steps for creating custom tag get from here

code for creating jsp custom tag with body

Tag implementation


TagBody.java

package st;

import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
import java.io.*;


public class TagBody extends TagSupport
{

     public int doStartTag() throws JspException
     {
        try{
               JspWriter out = pageContext.getOut();
               out.println( "<table border=1>");
           
                      out.println("<tr><td>  ");
                  

            }
        catch(Exception ex)
        {
        }
        return EVAL_BODY_INCLUDE;
     }

    public int doEndTag() throws JspException

    {
        try{
               JspWriter out = pageContext.getOut();
           
            out.println("</td></tr>");
               out.println("</table>");
        }
        catch(Exception ex)
        {
        }
        return EVAL_PAGE;
   
    }
}


Tag link library st.tld

<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE taglib
        PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"
    "http://java.sun.com/j2ee/dtd/web-jsptaglibrary_1_2.dtd">
<taglib>
  <tlib-version>1.0</tlib-version>
  <jsp-version>2.0</jsp-version>
  <short-name>st</short-name>
  <tag>
     <name>time</name>
     <tag-class>st.CurrentTime</tag-class>
     <body-content>empty</body-content>
  </tag> 
     <tag>
     <name>Hello</name>
     <tag-class>st.HelloTag</tag-class>
     <body-content>empty</body-content>
  </tag> 
  
 
    <tag>
     <name>myTag</name>
     <tag-class>st.myTag</tag-class>
     <body-content>empty</body-content>
  </tag>

<tag>
     <name>bodyTag</name>
     <tag-class>st.BodyTag</tag-class>
     <body-content>jsp</body-content>
  </tag>

<tag>
     <name>Tagbody</name>
     <tag-class>st.TagBody</tag-class>
     <body-content>jsp</body-content>
  </tag>
     

  <tag>
       <name>formattime</name>
       <tag-class>st.FormatCurrentTime</tag-class>
       <body-content>empty</body-content>
       <attribute>
          <name>format</name>
          <required>false</required>
       </attribute>
    </tag>      
 
</taglib>

JSP code

<%@page language="java" contentType="text/html" %>

<%@ taglib uri="/WEB-INF/tlds/st.tld" prefix="my"%>



<html>
    <body>
        <my:Tagbody>
            SS College
        </my:Tagbody>
    </body>
       
</html>

Output

jsp custome tag output
JSP custom tag with body

No comments:

Post a Comment