- Java提供两种实现,一种是实现Tag接口(麻烦);一种是实现SimpleTag,这种简单,我们仅需继承其实现类SimpleTagSupport来实现。下面就是采用第二种。
- 以下文章仅做提炼,无过多注释说明。
1 定义简单标签库:
项目结构如下:
第一步:定义标签库对应的MyTag.java类如下:
public class MyTag extends SimpleTagSupport {
@Override
public void doTag() throws JspException, IOException {
getJspContext().getOut().write("hello walker");
}
}
第二步:定义tld标签文件(当前标签放在webapp/WEB-INF目录下的任一子目录即可):
<?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/j2eehttp://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
version="2.0">
<description>A tag library exercising SimpleTag handlers.</description>
<tlib-version>1.0</tlib-version>
<short-name>SimpleTagLibrary</short-name>
<uri>mytid</uri>
<tag>
<description>Outputs a colored tile</description>
<name>hello</name>
<tag-class>com.lisam.tag.MyTag</tag-class>
<body-content>empty</body-content>
</tag>
</taglib>
第三步:修改index.jsp并引用自定义的标签:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib uri="mytid" prefix="mytags" %>
<html>
<head>
<title></title>
</head>
<body>
<mytags:hello/>
</body>
</html>
启动效果如下:
2 开发带属性标签库
实现步骤与"1"一致。
第一步:定义标签库对应的MyTag2.java类如下:
public class MyTag2 extends SimpleTagSupport {
private String map;
public String getMap() {
return this.map;
}
public void setMap(String map) {
this.map = map;
}
@Override
public void doTag() throws JspException, IOException {
HashMap<String, Integer> maps = (HashMap<String, Integer>) (getJspContext().getAttribute(map));
for (String str : maps.keySet()) {
getJspContext().getOut().write(str);
getJspContext().getOut().write("--");
getJspContext().getOut().write("" + maps.get(str));
getJspContext().getOut().write("</br>");
}
}
}
仅修改里面的tag部分即可
<tag>
<description>Outputs a colored tile</description>
<name>hello</name>
<tag-class>com.lisam.tag.MyTag2</tag-class>
<body-content>empty</body-content>
<attribute>
<name>map</name>
<required>true</required>
<fragment>true</fragment>
</attribute>
</tag>
仅修改body里面部分即可
<%
HashMap<String, Integer> map = new HashMap<String, Integer>();
map.put("李四", 53);
map.put("张三", 23);
map.put("walker", 22);
pageContext.setAttribute("map", map);
%>
<table>
map="map"/>
</table>
结果如下:
3 开发带标签体的标签库(这个最有用,可以做工具标签如权限、数值转换等)
实现步骤与"1"一致。
第一步:定义标签库对应的MyTag3.java类如下:
public class MyTag3 extends SimpleTagSupport {
private String map;
public String getMap() {
return this.map;
}
public void setMap(String map) {
this.map = map;
}
@Override
public void doTag() throws JspException, IOException {
HashMap<String, Integer> maps = (HashMap<String, Integer>) (getJspContext().getAttribute(map));
for (String str : maps.keySet()) {
getJspContext().setAttribute("name", str);
getJspContext().setAttribute("age", maps.get(str));
getJspBody().invoke(null);
}
}
}
第二步:定义tld标签文件(当前标签放在webapp/WEB-INF目录下的任一子目录即可):仅修改里面的tag部分即可
<tag>
<description>带标签的标签库</description>
<name>hello</name>
<tag-class>com.lisam.tag.MyTag3</tag-class>
<body-content>scriptless</body-content>
<attribute>
<name>map</name>
<required>false</required>
<fragment>true</fragment>
</attribute>
</tag>
第三步:修改index.jsp并引用自定义的标签:仅修改body里面部分即可
<%
HashMap<String, Integer> maps = new HashMap<String, Integer>();
maps.put("李四", 53);
maps.put("张三", 23);
maps.put("walker", 22);
pageContext.setAttribute("map", maps);
%>
<table>
<mytag:hello map="map">
<tr>
<td>${name2}</td>
<td>${age}</td>
</tr>
</mytag:hello>
</table>
结果如下: