自定义标签TagSupport

本文详细介绍了如何在JSP页面中通过自定义标签来展示数据,包括导入jar包、编写功能类、配置tld文件、web.xml引入及页面引用等步骤,实现了数据的有效展示。

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>

 

转载于:https://www.cnblogs.com/er123/p/5180212.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值