首页 > 其他 > 详细

自定义标签TagSupport

时间:2016-02-04 09:46:00      阅读:276      评论:0      收藏:0      [点我收藏+]

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>

 

自定义标签TagSupport

原文:http://www.cnblogs.com/er123/p/5180212.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!