zoukankan      html  css  js  c++  java
  • 自定义标签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>

  • 相关阅读:
    深圳成为全球第一个100%电动公共汽车的城市
    layui 数据表格按钮事件绑定和渲染
    Layui 改变数据表格样式覆盖
    js 遍历删除数组
    layui 数据表格最简单的点击事件
    layui 数据表格使用
    Layui 解决动态图标不动的问题
    Js 改变时间格式输出格式
    PHP 面向对象的数据库操作
    PHP SQL预处理
  • 原文地址:https://www.cnblogs.com/er123/p/5180212.html
Copyright © 2011-2022 走看看