最后蓝色粗体的是实现的类,前面的都是接口。
7. SimpleTag示例
/*标签程序,继承了SimpleTagSupport类*/
import java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;
/**
* 仅以此代码纪念2008年5月12号的四川汶川大地震
* 愿生者顽强,死者安息
* @author liky
*
*/
public class EarthquakeTag extends SimpleTagSupport {
private String name;
public void setName(String name) {
this.name = name;
}
public void doTag() throws JspException, IOException {
super.doTag();
this.getJspContext().getOut().write(name + "同志,请向四川灾区人民奉献您的一份爱心!");
}
}
/*标签库描述符*/
<?xml version="1.0" encoding="ISO-8859-1" ?>
<taglib xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee web-jsptaglibrary_2_0_xsd"
version="2.0">
<description>
A tag extends SimpleTagSupport for instance.
</description>
<tlib-version>1.0</tlib-version>
<short-name>quake</short-name>
<uri>/simple</uri>
<tag>
<description>demo the simple tag</description>
<name>quake</name>
<tag-class>com.taglib.EarthquakeTag</tag-class>
<body-content>empty</body-content>
<attribute>
<name>name</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>
/*web.xml布署配置*/
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<jsp-config>
<taglib>
<taglib-uri>/WEB-INF/simpleTag.tld</taglib-uri>
<taglib-location>/WEB-INF/simpleTag.tld</taglib-location>
</taglib>
</jsp-config>
</web-app>
/*以下JSP代码代码测试调用*/
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/WEB-INF/c.tld" prefix="c"%>
<%@ taglib uri="/WEB-INF/earthquake.tld" prefix="e" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>测试重新配置标准标签库</title>
</head>
<body>
<e:quake name="比尔.盖子"/>
</body>
</html>
8. BodyTag的示例
/*一个类继承自BodyTagSupport*/
package com.taglib;
import java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.tagext.BodyContent;
import javax.servlet.jsp.tagext.BodyTagSupport;
/**将标签体内的文字,使用指定的颜色和次数输出,每输出一个都换行*/
public class DedicateTag extends BodyTagSupport {
String color;
int count;
public void setColor(String color) {
this.color = color;
}
public void setCount(int count) {
this.count = count;
}
@Override
public int doAfterBody() throws JspException {
if (count > 1) {
count --;
try {
pageContext.getOut().write("<br>");
} catch (IOException e) {
throw new JspTagException ("IO Error: " + e.getMessage());
}
return EVAL_BODY_AGAIN;
}
else {
return SKIP_BODY;
}
}
@Override
public int doEndTag() throws JspException {
try {
if (bodyContent != null) {
bodyContent.writeOut(bodyContent.getEnclosingWriter());
pageContext.getOut().write("</font><br>");
}
} catch (IOException e) {
throw new JspTagException ("IO Error: " + e.getMessage());
}
return EVAL_PAGE;
}
@Override
public void doInitBody() throws JspException {
super.doInitBody();
}
@Override
public int doStartTag() throws JspException {
if(count > 0) {
try {
pageContext.getOut().write("<font color=" + color + ">");
} catch (Exception e) {
e.printStackTrace();
}
return EVAL_BODY_INCLUDE;
}
else {
return SKIP_BODY;
}
}
}
/*标签库描述符*/
<?xml version="1.0" encoding="ISO-8859-1" ?>
<taglib xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee web-jsptaglibrary_2_0_xsd"
version="2.0">
<description>
A tag extends SimpleTagSupport for instance.
</description>
<tlib-version>1.0</tlib-version>
<short-name>dedicate</short-name>
<uri>/dedicate</uri>
<tag>
<description>demo the body tag</description>
<name>dedicate</name>
<tag-class>com.taglib.DedicateTag</tag-class>
<body-content>jsp</body-content>
<attribute>
<name>color</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>count</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>
/*web.xml部署描述*/
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<jsp-config>
<taglib>
<taglib-uri>/WEB-INF/dedicate.tld</taglib-uri>
<taglib-location>/WEB-INF/dedicate.tld</taglib-location>
</taglib>
</jsp-config>
</web-app>
/*测试用的JSP页面*/
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/WEB-INF/dedicate.tld" prefix="d"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>测试重新配置标准标签库</title>
</head>
<body>
<d:dedicate color="#AA1234" count="6">2008年5月12号</d:dedicate>
</body>
</html>