JSTL 简单的自定义标签 class simpleTagSupport

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/>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

友情链接更多精彩内容