一、实现类
package com.want.mytag;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyContent;
import javax.servlet.jsp.tagext.BodyTagSupport;
import java.io.IOException;
/**
* Created by want on 2016/12/12.
*/
public class MyBodyTag extends BodyTagSupport {
private String name;
private String tel;
public void setName(String name) {
System.out.println("[setName]name = " + name);
this.name = name;
}
public void setTel(String tel) {
System.out.println("[setTel]tel = " + tel);
this.tel = tel;
}
public String getName() {
System.out.println("[getName]");
return name;
}
public String getTel() {
System.out.println("[getTel]");
return tel;
}
public MyBodyTag() {
super();
System.out.println("MyBodyTag构造方法");
}
@Override
public int doEndTag() throws JspException {
System.out.println("doEndTag()");
return super.doEndTag();
}
@Override
public void doInitBody() throws JspException {
//如果标签没有内容将不执行,<body-content>empty</body-content>此时会出现空指针异常
System.out.println("doInitBody()");
super.doInitBody();
}
@Override
public int doStartTag() throws JspException {
System.out.println("doStartTag()");
return super.doStartTag();
}
public int doAfterBody() throws JspException {
try {
BodyContent bodycontent = getBodyContent();
System.out.println("bodycontent" + bodycontent);
String body = bodycontent.getString();
body+=":"+getName()+":"+getTel();
System.out.println("body" + body);
JspWriter out = bodycontent.getEnclosingWriter();
System.out.println("out" + out);
if (body != null) {
out.print(body.toUpperCase());
}
} catch (IOException ioe) {
throw new JspException("Error:" + ioe.getMessage());
}
return SKIP_BODY;
}
}
二、.tld文件
<?xml version="1.0" encoding="UTF-8"?>
<!-- edited with XML Spy v4.1 U (http://www.xmlspy.com) by À¼ÌØ (LanTe) -->
<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"
"http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">
<!-- template test -->
<taglib>
<tlib-version>1.0</tlib-version>
<jsp-version>1.2</jsp-version>
<short-name>want</short-name>
<tag>
<name>btag</name>
<tag-class>com.want.mytag.MyBodyTag</tag-class>
<!--empty:空标记,即起始标记和结束标记之间没有内容。-->
<!--<body-content>empty</body-content>-->
<!--JSP:接受所有JSP语法,如定制的或内部的tag、scripts、静态HTML、脚本元素、JSP指令和动作。-->
<body-content>JSP</body-content>
<!--tagdependent:标签体内容直接被写入BodyContent,由自定义标签类来进行处理,而不被JSP容器解释,-->
<!-- <body-content>tagdependent</body-content>-->
<!--scriptless:接受文本、EL和JSP动作-->
<!-- <body-content>scriptless</body-content>-->
<attribute>
<name>name</name>
<required>true</required>
</attribute>
<attribute>
<name>tel</name>
<required>true</required>
</attribute>
</tag>
</taglib>
三、jsp页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="tt" uri="/WEB-INF/mybodytag.tld" %>
<html>
<head>
<title>标签2</title>
</head>
<body>
标签:<br>
<tt:btag name="小王" tel="123456780">标签A</tt:btag><br>
<tt:btag name="小李" tel="6542130">标签B</tt:btag><br>
<tt:btag name="空的" tel="0000"></tt:btag><br>
</body>
</html>
四、显示效果