1,写自定义标签类对象(MyTag.java)
package com.itheima.crm.utils;
import java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;
/**
* 自定义标签的使用
* @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>");
out.print("<p>这个是第一段</p>");
} catch (IOException e) {
e.printStackTrace();
}
return super.doStartTag();//获取上下文
}
}
2,自定义标签描述文件(/WebContent/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/"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page trimDirectiveWhitespaces="true"%>
<%@ taglib prefix="myTag" uri="http://itheima/myTag/"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<!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>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<meta name="author" content="">
<title>自定义标签的使用</title>
</head>
<body>
</body>
<myTag:out></myTag:out>
</html>