版权声明:龙人版 https://blog.csdn.net/qq_39021393/article/details/90706716
自定义标签类
MyTag.java
public class MyTag extends TagSupport{
@Override
public int doStartTag() throws JspException {
JspWriter out = super.pageContext.getOut();
try {
out.print("<span>这个是自定义标签输出的内容</span>");
out.print("<h1>这个是自定义标签输出的大标题</h1>");
} catch (IOException e) {
e.printStackTrace();
}
return super.doStartTag();
}
}
继承TagSupport类,实现自定义标签类
重写doStartTag()方法
pageContext():代表JSP页面的运行环境
getOut():请求的目标资源处理的输出通过调用getOut()返回当前JspWriter
out.print():向JSP页面输出内容
定义描述文件
myTag.tld
<?xml version="1.0" encoding="UTF-8" ?>
<!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>2.0</tlib-version>
<jsp-version>1.2</jsp-version>
<short-name>myTag</short-name>
<uri>http://xlr/myTag/</uri>
<tag>
<name>out</name>
<tag-class>ink.xlr.crm.util.MyTag</tag-class>
<body-content>JSP</body-content>
</tag>
</taglib>
tag-class:定位自定义标签类位置
short-name:为标签前缀
name:为前缀后的名称
测试标签
myTag.jsp
<%@ taglib prefix="myTag" uri="http://xlr/myTag/"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" isELIgnored="false"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>自定义标签</title>
</head>
<body>
<myTag:out></myTag:out>
</body>
</html>
uri为自定义描述文件的内容
myTag为配置中的前缀名称