吴裕雄--天生自然JAVA开发JSP-Servlet学习笔记:自定义标签-在页面输出HelloWorld

import java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;

public class HelloWorldTag extends SimpleTagSupport {
    // 重写doTag()方法,该方法为标签生成页面内容

    public void doTag() throws JspException, IOException {
        // 获取页面输出流,并输出字符串
        getJspContext().getOut().write("Hello World "+ new java.util.Date());
    }
}
<?xml version="1.0" encoding="UTF-8"?>

<taglib xmlns="http://xmlns.jcp.org/xml/ns/javaee"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee 
    http://xmlns.jcp.org/xml/ns/javaee/web-jsptaglibrary_2_1.xsd"
        version="2.1">
    <tlib-version>1.0</tlib-version>
    <short-name>mytaglib</short-name>
    <!-- 定义该标签库的URI -->
    <uri>http://www.crazyit.org/mytaglib</uri>

    <!-- 定义第一个标签 -->
    <tag>
        <!-- 定义标签名 -->
        <name>helloWorld</name>
        <!-- 定义标签处理类 -->
        <tag-class>HelloWorldTag</tag-class>
        <!-- 定义标签体为空 -->
        <body-content>empty</body-content>
    </tag>
</taglib>

猜你喜欢

转载自www.cnblogs.com/tszr/p/12688100.html