JSP custom tags example with body 

Joined:
08/13/2009
Posts:
164

January 28, 2012 13:24:09    Last update: January 28, 2012 13:31:22
This is a simple JSP custom tags library with tag body. Just like the JSF counterpart, it splits a string and repeats the body for each word, i.e., with this markup:
<%@ taglib uri="http://custom.tag.com/demo" prefix="my"%>
<html>
<body>
<my:iterate var="name" value="Tigger Kanga Roo">
	<p>Hello ${name}!</p>
</my:iterate>
</body>
</html>

output:
<html>
<body>
	<p>Hello Tigger!</p>
	<p>Hello Kanga!</p>
	<p>Hello Roo!</p>
</body>
</html>

With Maven, this is the directory structure:
./src
./src/main
./src/main/resources
./src/main/resources/META-INF
./src/main/resources/META-INF/demotag.tld
./src/main/java
./src/main/java/tagdemo
./src/main/java/tagdemo/IterateTag.java
./pom.xml

There are three files to write:
  1. pom.xml:
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <groupId>tag-demo</groupId>
        <artifactId>jsp-tags</artifactId>
        <packaging>jar</packaging>
        <version>1.0</version>
        <name>JSP Custom tag demo</name>
        <url>http://custom.tag.com</url>
    
        <dependencies>
    	<dependency>
    	    <groupId>javax</groupId>
    	    <artifactId>javaee-api</artifactId>
    	    <version>6.0</version>
    	    <scope>provided</scope>
    	</dependency>
        </dependencies>
    </project>
    

  2. src/main/java/tagdemo/IterateTag.java:
    package tagdemo;
    
    import java.io.IOException;
    
    import javax.servlet.jsp.JspException;
    import javax.servlet.jsp.tagext.TagSupport;
    import javax.servlet.jsp.tagext.BodyContent;
    import javax.servlet.jsp.tagext.BodyTagSupport;
    
    public class IterateTag extends BodyTagSupport {
    	private String value;
    	private String var;
    	private String[] vals = null;
    	private int index = 0;
    
    	public String getValue() {
    		return this.value;
    	}
    
    	public void setValue(String value) {
    		this.value = value;
    		this.vals = value.split("\\s+");
    	}
    
    	public String getVar() {
    		return this.var;
    	}
    
    	public void setVar(String var) {
    		this.var = var;
    	}
    
    	public int doStartTag() throws JspException {
    		if (this.vals.length < 1) {
    			return SKIP_BODY;
    		}
    
    		pageContext.setAttribute(getVar(), vals[0]);
    		this.index = 1;
    		return EVAL_BODY_BUFFERED;
    	}
    
    	public int doAfterBody() throws JspException {
    		if (this.index < this.vals.length) {
    			pageContext.setAttribute(getVar(), vals[this.index]);
    			this.index++;
    			return EVAL_BODY_AGAIN;
    		}
    
    		try {
    			BodyContent bc = getBodyContent();
    			bc.writeOut(getPreviousOut());
    		}
    		catch (IOException e) {
    			throw new JspException(e);
    		}
    
    		return SKIP_BODY;
    	}
    }
    

  3. src/main/resources/META-INF/demotag.tld:
    <?xml version="1.0" encoding="UTF-8"?>
    
    <!DOCTYPE taglib 
        PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"
        "http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">
    <taglib>
        <tlib-version>1.0</tlib-version>
        <jsp-version>1.2</jsp-version>
        <short-name>cust</short-name>
        <uri>http://custom.tag.com/demo</uri>
        <display-name>Custom Tag Example</display-name>
        <description>Custom tag examples</description>
    
        <tag>
    	<name>iterate</name>
            <tagclass>
                tagdemo.IterateTag
            </tagclass>
            <bodycontent>JSP</bodycontent>
            <description>
                <![CDATA[
                <p><strong>Split a String and repeat body for each</strong></p>
                ]]>
            </description>
            <attribute>
                <name>value</name>
                <required>true</required>
                <rtexprvalue>true</rtexprvalue>
                <description>
                    <![CDATA[
                      <p>The String value to split</p>
                      ]]>
                </description>
            </attribute>
            <attribute>
                <name>var</name>
                <required>true</required>
                <rtexprvalue>true</rtexprvalue>
                <description>
                    <![CDATA[
                      <p>Name of the variable for each iteration.</p>
                    ]]>
                </description>
            </attribute>
        </tag>
    </taglib>
    



Build with:
mvn clean install


To use it as a dependency in other Maven projects:
<dependency>
	    <groupId>tag-demo</groupId>
	    <artifactId>jsp-tags</artifactId>
	    <version>1.0</version>
	    <scope>runtime</scope>
</dependency>
Share |
| Comment  | Tags