• 自定义标签TagSupport


    1、导入jar 包

    jsp-api-2.2-sources.jar

    2、编写功能类

    public class TagTest extends TagSupport{


    private static final long serialVersionUID = 1L;
    private int num;

    public int doStartTag()throws JspException{

    Map<Integer, String> maps = new HashMap<>();
    maps.put(1, "张三");
    maps.put(2, "李四");
    try{
    super.pageContext.getOut().write(maps.get(num));

    }catch(Exception e){
    e.printStackTrace();
    }finally {
    return super.doStartTag();
    }
    }

    public int getNum() {
    return num;
    }

    public void setNum(int num) {
    this.num = num;
    }
    }

    3、配置tld文件

    <?xml version="1.0" encoding="UTF-8" ?>

    <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>there are custom tags of tag</description>
    <tlib-version>1.0</tlib-version>
    <short-name>test</short-name>
    <uri>test</uri>

    <tag>
    <description>自定义标签</description>
    <name>test</name>
    <tag-class>web.tag.TagTest</tag-class>
    <body-content>empty</body-content>
    <attribute>
    <description>描述</description>
    <name>num</name>
    <required>true</required>
    <rtexprvalue>true</rtexprvalue>
    </attribute>

    </tag>

    </taglib>

    4、web.xml中引入

    <jsp-config>
    <taglib>
    <taglib-uri>/test</taglib-uri>
    <taglib-location>/WEB-INF/showTag.tld</taglib-location>
    </taglib>
    </jsp-config>

    5、页面引用

    <%@ taglib prefix="ws" uri="/test"%>

    <p><ws:test num="1"/></p>

  • 相关阅读:
    vc枚举本机端口信息API
    xmpp 与服务器连接并身份验证成功
    xmpp 常见错误 一
    xmpp 环境配置
    使用宏定义创建单例
    iOS知识列表
    ios sqlite 简单使用
    关于游戏
    开通博客园博客的第一天
    如何让移动设备的宽与浏览器视图的宽保持一致
  • 原文地址:https://www.cnblogs.com/er123/p/5180212.html
Copyright © 2020-2023  润新知