HelloWorldTag.java
package com.ruanku.tag;
import java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;
public class HelloWorldTag extends TagSupport{
/**
*
*/
private static final long serialVersionUID = 1L;
@Override
public int doStartTag() throws JspException {
JspWriter out=this.pageContext.getOut();
try {
out.println("这是 jsp标签");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return TagSupport.SKIP_BODY;//直接结束标签
}
}
cc.tld
<?xml version="1.0" encoding="UTF-8" ?>
<taglib xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd"
version="2.1">
<tlib-version>1.0</tlib-version>
<short-name>ccTag</short-name>
<tag>
<name>helloWorld</name>
<tag-class>com.ruanku.tag.HelloWorldTag</tag-class>
<body-content>empty</body-content>
</tag>
</taglib>
HelloWorldTag.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ taglib prefix="cc" uri="/WEB-INF/cc.tld" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<cc:helloWorld/>
</body>
</html>