Custom Tags tutorial

It's very simple and not full. The simple process is:

1. Write class: the class usually inherit (extend) TagSupport (or some other). The functions below are useful:

public int doEndTag()
             throws JspException
public int doStartTag()
               throws JspException
the example is below:
public class DemoTag extends TagSupport {
 private String name = "oriName";
 public int doStartTag() throws JspException {
  //HttpServletRequest request = (HttpServletRequest)pageContext.getRequest();
  JspWriter out = pageContext.getOut();
  try {
   out.println("Tags Entered...........");
   out.println("Name is " + name + "..............");
  } catch (IOException e) {
  return SKIP_BODY;
 public String getName() {
  return name;
 public void setName(String name) { = name;

2. define tld (tag library define) file, below is one example, the header of the file is important:

<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE taglib
    PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2/EN"


On above, the <attribut> is the point deciding if the tag has parameters.

3. Use the tag in JSP:

3.1 declear the taglib, like:

<%@ taglib uri="/WEB-INF/myTagLib.tld" prefix="myTag" %>

3.2 use:

<myTag:demoTag name="changedName"/>


4. OK :)


上一篇Design Patterns -- Facade
下一篇Design Patterns -- Bridge
想对作者说点什么? 我来说一句


2011年06月22日 172KB 下载