定义简单标签类JSTL
* 1、创建Java文件,从javax.servlet.jsp.tagext.SimpleTagSupport类继承(注意次Java文件不允许裸类,即必须在包内)
* 2、重写doTag方法,使用getJspContext().getOut().write("HelloWorld!");输出
package mytag;
<%@ taglib uri="/helloworldtaglib" prefix="mytag" %>
*/
import java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;
public class HelloTag extends SimpleTagSupport {
@Override
public void doTag() throws JspException, IOException {
this.getJspContext().getOut().write("HelloWorld!");
}
}
* 3、在web-inf或其子目录下创建helloworld.tld文件,即自定义标签的说明文件
<?xml version="1.0" encoding="UTF-8"?>
<taglib xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.rog/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
version="2.0" >
<tlib-version>1.0</tlib-version>
<short-name>mytag</short-name>
<uri>/helloworldtaglib</uri>
<tag>
<name>helloworld</name>
<tag-class>mytag.HelloTag</tag-class>
<body-content>empty</body-content>
</tag>
</taglib>
* 4、建立TestTag.jsp测试文件<%@ taglib uri="/helloworldtaglib" prefix="mytag" %>
<%@ page language="java" contentType="text/html; charset=gbk"
pageEncoding="gbk"%>
<%@ taglib uri="/helloworldtaglib" prefix="mytag" %>
<!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=gbk">
<title>Insert title here</title>
</head>
<body>
<mytag:helloworld />
</body>
</html>