美文网首页
JSTL 简单的自定义标签 class simpleTagSu

JSTL 简单的自定义标签 class simpleTagSu

作者: SmallTwo | 来源:发表于2017-10-06 00:40 被阅读66次

1 。 创建一个类实现 SimpleTag 接口

public class customTagLib implements SimpleTag {

    
    
    
    @Override
    public void doTag() throws JspException, IOException {
        // TODO Auto-generated method stub
        
    }

    @Override
    public void setParent(JspTag parent) {
        // TODO Auto-generated method stub
        
    }

    @Override
    public JspTag getParent() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void setJspContext(JspContext pc) {
        // TODO Auto-generated method stub
        
    }

    @Override
    public void setJspBody(JspFragment jspBody) {
        // TODO Auto-generated method stub
        
    }

    
}

  1. 在dotag方法中编写自己想要实现的内容
    @Override
    public void doTag() throws JspException, IOException {
        // TODO Auto-generated method stub
        JspWriter out = context.getOut();
        out.print("这个是我的第一个自定义的标签");
    

3。 创建tld文件

<taglib xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/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">
    <description>A tag library exercising SimpleTag handlers.</description>
    <tlib-version>1.0</tlib-version>
    <short-name>pkuie</short-name>
    <uri>http://www.baidu.com/pkuie</uri>
    <tag>
        <name>hello</name>
        <tag-class>jsp.customTagLib</tag-class>
        <body-content>empty</body-content>
    </tag>

</taglib>
  1. 使用
<%@ taglib prefix="pkuie" uri="http://www.baidu.com/pkuie"%>

<pkuie:hello/>

相关文章

网友评论

      本文标题:JSTL 简单的自定义标签 class simpleTagSu

      本文链接:https://www.haomeiwen.com/subject/tnqlyxtx.html