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) {
      e.printStackTrace();
  }
 
  return SKIP_BODY;
 }
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.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"
           "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>site-utils</short-name>
  <tag>
      <name>demoTag</name>
      <tag-class>com.tcs.tags.DemoTag</tag-class>
      <body-content>empty</body-content>
      <attribute>
      <name>name</name>
      </attribute>
    </tag>
</taglib>

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 :)

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值