自定义标签TagSupport-飞外

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