Friday, November 14, 2014

Spring MVC custom body tag library example


Create two java file with following contents:

package com.pkm.maven.tag;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyContent;
import javax.servlet.jsp.tagext.BodyTagSupport;

public class BodyTagOne extends BodyTagSupport {
    private int mTimes = 0;
    private BodyContent mBodyContent;
    
    public void setTimes(int pTimes) {
        mTimes = pTimes;
    }
    
    @Override
    public void setBodyContent(BodyContent pBodyContent) {
        mBodyContent = pBodyContent;
    }
    
    @Override
    public int doStartTag() throws JspException {
        if (mTimes >= 1) {
            return EVAL_BODY_TAG;
        } else {
            return SKIP_BODY;
        }
    }
    
    @Override
    public int doAfterBody() throws JspException {
        if (mTimes > 1) {
            mTimes--;
            return EVAL_BODY_TAG;
        } else {
            return SKIP_BODY;
        }
    }

    
    @Override
    public int doEndTag() throws JspException {
        try {
            if (mBodyContent != null) {
                mBodyContent.writeOut(mBodyContent.getEnclosingWriter());
            }
        } catch (Exception pIOEx) {
            throw new JspException("Error: " + pIOEx.getMessage());
        }
        return EVAL_PAGE;
    }
}





package com.pkm.maven.tag;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyContent;
import javax.servlet.jsp.tagext.BodyTagSupport;

public class BodyTagTwo extends BodyTagSupport {    
    @Override
    public int doAfterBody() throws JspException {
        try {
            BodyContent bc = getBodyContent();
            String body = bc.getString();
            JspWriter out = bc.getEnclosingWriter();
            if (body != null) {
                body = body.substring(0, 1).toUpperCase() + body.substring(1);
                out.print(body);
            }
        } 
        catch (Exception ioe) {
            throw new JspException("Error: " + ioe.getMessage());
        }
        return SKIP_BODY;
    }
}

Create the tag library descriptor (TLD) under WEB-INF folder suppose named: "UiTabLib.tld":

<taglib xmlns="http://java.sun.com/xml/ns/j2ee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
         version="2.0">
 
     <tlib-version>1.0</tlib-version>
     <short-name>Ui Tab Library</short-name>
     <uri>UiTabLib</uri>
 
     <tag>
        <name>loopText</name>
        <tag-class>com.pkm.maven.tag.BodyTagOne</tag-class>
        <body-content>JSP</body-content>
        <info>This Tag Displayes given text multiple times</info>
        <attribute>
            <name>times</name>
            <required>true</required>
            <description>Provide number of times the text will be repeated</description>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
    </tag>
     <tag>
        <name>capitalize</name>
        <tag-class>com.pkm.maven.tag.BodyTagTwo</tag-class>
        <body-content>JSP</body-content>
        <info>This Tag Displays given string to be capitalized</info>
    </tag>
</taglib>

Reference & use the tag library:

<p>
    Body Tag Example: <br/>
    <% Integer counter = 1; %>
    <UiTabLib:loopText times="10">
        Counter: <%= (counter++) %><br/>
    </UiTabLib:loopText>
   <%= counter %>
</p>
<p>
    String capitalize (text to be capitalized): 
    <UiTabLib:capitalize>text to be capitalized</UiTabLib:capitalize>
</p>

Output would be like this:

Body Tag Example: 
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 5
Counter: 6
Counter: 7
Counter: 8
Counter: 9
Counter: 10
11
String capitalize (text to be capitalized): Text to be capitalized

No comments:

Post a Comment