自定义标签

1、编写自定义标签类
/**
* 自定义标签的使用
* @author Steven
*
*/
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();
}
}

2、编写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://itheima/myTag/</uri>
    <tag>
    	    <name>out</name>
	    <tag-class>com.itheima.crm.utils.MyTag</tag-class>
	    <body-content>JSP</body-content>
    </tag>
</taglib>

3、使用自定义标签,页面引入
<%@ taglib prefix="myTag" uri="http://itheima/myTag/"%>
myTag:out</myTag:out>

posted @ 2020-04-23 10:29  luoqi123456  阅读(149)  评论(0编辑  收藏  举报