Making of Custom TagLib in JSP

<%-- 
    Document   : index
    Created on : Sep 23, 2011, 6:05:28 PM
    Author     : Taher_JAVAHUNTER
--%>
<!--Author : Taher_JavaHunter-->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    <%@taglib prefix="tt" uri="http://www.javahunter.taher.com"%>
    <head>
        <title></title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <script type="text/javascript" src="jquery-1.4.3.min.js"></script>
        <script type="text/javascript">            
            function txtBoxEmpty(control,length,srno){
                var tbool=true;
                $(".err"+srno).remove();
                if($.trim($(control).val())==''){
                    $(control).parent().append("<div class='err"+srno+"' style='color:red;'>Please Enter Value</div>");
                    tbool = false;
                }else{
                    if(length!=0){
                        if($(control).val().length>length){
                            $(control).parent().append("<div class='err"+srno+"' style='color:red;'>Max "+length+" Characters Allowed</div>");
                            tbool = false;
                        }
                    }
                }
                return tbool;
            }
        </script>
    </head>
    <body>
        <form>
            <table border="5" align="center">
                <tr>
                    <td>Name : </td>
                    <td>
                        <tt:inputText fieldid="test" fieldname="test" length="5" componentno="1"/>
                    </td>
                </tr>                
                <tr>
                    <td>Surname : </td>
                    <td>
                        <tt:inputText fieldid="test" fieldname="test" length="10" componentno="2"/>
                    </td>
                </tr>
            </table>
        </form>
    </body>
</html>

TabLib Class : InputTextHandler.java

package com.taher.customtag;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

public class InputTextHandler extends TagSupport {
  private String fieldid;
  private String fieldname;
  private String fieldvalue="";
  private String length;
  private String componentno;

    public String getFieldname() {
        return fieldname;
    }

    public void setFieldname(String fieldname) {
        this.fieldname = fieldname;
    }

    public String getFieldvalue() {
        return fieldvalue;
    }

    public void setFieldvalue(String fieldvalue) {
        this.fieldvalue = fieldvalue;
    }

    public String getFieldid() {
        return fieldid;
    }

    public void setFieldid(String fieldid) {
        this.fieldid = fieldid;
    }

    public String getComponentno() {
        return componentno;
    }

    public void setComponentno(String componentno) {
        this.componentno = componentno;
    }

    public String getLength() {
        return length;
    }

    public void setLength(String length) {
        this.length = length;
    }

    @Override
    public int doStartTag() throws JspException {

        try {
            JspWriter out = pageContext.getOut();
            out.println("<input type='text' id='"+fieldid+"' name='"+fieldname+"' value='"+fieldvalue+"' onblur='txtBoxEmpty(this,"+length+","+componentno+")'/>");
        } catch (IOException e) {
            System.out.println("Error : "+e);
        }
        return SKIP_BODY;
    }    
}

TLD to be added to WEB-INF : InputTextDescriptor.tld

<?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>substrdescriptor</short-name>
    <uri>http://www.javahunter.taher.com</uri>
    <tag>
        <name>inputText</name>
        <tagclass>com.taher.customtag.InputTextHandler</tagclass>
        <info>Html Input Text.</info>
        <attribute>
            <name>fieldid</name>
            <required>true</required>
        </attribute>
        <attribute>
            <name>fieldname</name>
            <required>true</required>
        </attribute>
        <attribute>
            <name>fieldvalue</name>
            <required>false</required>
        </attribute>
        <attribute>
            <name>length</name>
            <required>true</required>
        </attribute>
        <attribute>
            <name>componentno</name>
            <required>true</required>
        </attribute>
    </tag>
</taglib>
Advertisements
This entry was posted in JSP, Just 4 Knowledge. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s