自定义JSP标签Taglib

1、AuthorTag.java

package com.ats.taglib;

import java.io.IOException;

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

public class AuthorTag extends TagSupport{
	private static final long serialVersionUID = 1L;
	private String version="v1.0";

	@Override
	public int doStartTag(){
		JspWriter out=pageContext.getOut();
		try {
			out.print("<div>AuthorTag "+version+" by NEOHOPE</div>");
		} catch (IOException e) {
			e.printStackTrace();
		}
		return EVAL_BODY_INCLUDE;
	}
	
	@Override
	public int doEndTag(){
		return EVAL_PAGE;
	}
	
	public String getVersion()
	{
		return version;
	}
	
	public void setVersion(String version)
	{
		this.version=version;
	}
}

2、IterateTag.java

package com.ats.taglib;

import java.io.IOException;
import java.util.Collection;
import java.util.Iterator;

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

public class IterateTag extends BodyTagSupport {
	private static final long serialVersionUID = 1L;

	private Iterator it;
	private String type;
	private String name;
	public void setCollection(Collection collection) {
		if (collection.size() > 0)
			it = collection.iterator();
	}
	
	public void setType(String type)
	{
		this.type=type;
	}
	
	public void setName(String name)
	{
		this.name=name;
	}

	@Override
	public int doStartTag() {
		
		JspWriter out=pageContext.getOut();
		try {
			out.print("<table>");
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		if (it == null)
			return SKIP_BODY;
		else
			return continueNext(it);
	}

	@Override
	public int doAfterBody() {
		return continueNext(it);
	}

	@Override
	public int doEndTag() {
		try {
			if (bodyContent != null)
				bodyContent.writeOut(bodyContent.getEnclosingWriter());
		} catch (Exception e) {
			System.out.println(e);
		}
		
		JspWriter out=pageContext.getOut();
		try {
			out.print("</table>");
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return EVAL_PAGE;
	}

	protected int continueNext(Iterator it) {
		if (it.hasNext()) {
			pageContext.setAttribute(name, "<tr><td>"+it.next()+"</td></tr>", pageContext.PAGE_SCOPE);
			return EVAL_BODY_TAG;
		} else {
			return SKIP_BODY;
		}
	}
}

3、IterateTagTEI.java

package com.ats.taglib;

import javax.servlet.jsp.tagext.TagData;
import javax.servlet.jsp.tagext.TagExtraInfo;
import javax.servlet.jsp.tagext.VariableInfo;

public class IterateTagTEI extends TagExtraInfo {
	public IterateTagTEI() {
		super();
	}

	@Override
	public VariableInfo[] getVariableInfo(TagData data) {
		return new VariableInfo[] { new VariableInfo(
				data.getAttributeString("name"),
				data.getAttributeString("type"), true, VariableInfo.NESTED) };
	}
}

4、LoopTag.java

package com.ats.taglib;

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

public class LoopTag extends BodyTagSupport {
	private static final long serialVersionUID = 1L;

	private int total = 0;
	private int count = 0;

	@Override
	public int doStartTag() {
		try {
			if (total > 0)
				return EVAL_BODY_TAG;
			else
				return SKIP_BODY;
		} catch (Exception e) {
			e.printStackTrace();
			return SKIP_BODY;
		}
	}

	@Override
	public int doAfterBody() {
		try {
			if (total - count>1) {
				count++;
				return EVAL_BODY_TAG;
			} 
			else
			{
				count=0;
				return SKIP_BODY;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return SKIP_BODY;
		}
	}

	@Override
	public int doEndTag() {
		try {
			bodyContent.writeOut(bodyContent.getEnclosingWriter());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return EVAL_PAGE;
	}
	
	public void setTotal(int total)
	{
		this.total = total;
	}
	
	public int getTotal()
	{
		return total;
	}
}

5、Taglib.tld

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN" "http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd"&#93;
<taglib>
	<tlibversion>1.0</tlibversion>
	<jspversion>1.1</jspversion>
	<shortname>at</shortname>
	<tag>
		<name>AuthorTag</name>
		<tagclass>com.ats.taglib.AuthorTag</tagclass>
		<bodycontent>empty</bodycontent>

		<attribute>
			<name>version</name>
			<required>false</required>
		</attribute>
	</tag>
	<tag>
		<name>LoopTag</name>
		<tagclass>com.ats.taglib.LoopTag</tagclass>
		<bodycontent>jsp</bodycontent>
		<attribute>
			<name>total</name>
			<required>true</required>
			<rtexprvalue>true</rtexprvalue>
		</attribute>
	</tag>
	<tag>
		<name>IterateTag</name>
		<tagclass>com.ats.taglib.IterateTag</tagclass>
		<teiclass>com.ats.taglib.IterateTagTEI</teiclass>
		<bodycontent>jsp</bodycontent>
		<attribute>
			<name>collection</name>
			<required>true</required>
			<rtexprvalue>true</rtexprvalue>
		</attribute>
		<attribute>
			<name>name</name>
			<required>true</required>
		</attribute>
		<attribute>
			<name>type</name>
			<required>true</required>
		</attribute>
	</tag>
</taglib>

6、web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"&#93;
  <display-name>TaglibTest</display-name>
  
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  
  <jsp-config>
    <taglib>
      <taglib-uri>
        http://www.ats.com/Taglib
      </taglib-uri>
      <taglib-location>
        /WEB-INF/tags/Taglib.tld
      </taglib-location>
	</taglib>
  </jsp-config>
</web-app>

7、index.jsp

<%@ taglib prefix="ats" uri="http://www.ats.com/Taglib" %>

<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@ page import="java.util.Date"%> 
<%@ page import="java.util.ArrayList"%> 

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>TaglibTest</title>
</head>

<body>
<ats:AuthorTag version="v1.01"/>
<ats:LoopTag total="3">
<div><%=new Date()%></div>
</ats:LoopTag>

<%
ArrayList<String> t=new ArrayList<String>();
t.add("A");
t.add("B");
t.add("C");
%>

<div>
<ats:IterateTag name="row" type="String" collection="<%=t%>">
<%=row%>
</ats:IterateTag>
</div>

</body>
</html>

Leave a Reply

Your email address will not be published. Required fields are marked *

*