tag 編寫一個tag涉及三個步驟,
(1)編寫jsp
(2)編寫tag的java程序
(3)編寫tag庫的描述文件tld(實際是一個xml文件)
這三個步驟之間沒有順序約束,下面是一個簡單的例子:
1 編寫hellotag.jsp
<%@page contenttype="text/html"%>
<html>
<head><title>hello tags page</title></head>
<body>
<%@ taglib uri="/web-inf/classes/tags/hellotag.tld" prefix="hello" %>
<hello:hellotag />
</body>
</html>
2 編寫tag
hellotag.java
package tags; //注意:必須放在一個包中
import java.io.*;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
public class hellotag extends tagsupport {
public int dostarttag() {
try {
//使用jspwriter獲得jsp的輸出對象
jspwriter jspwriteroutput = pagecontext.getout();
jspwriteroutput.print("hello tag!");
} catch (ioexception ioex) {
system .out.println("ioexception in hellotag " + ioex);
}
return (skip_body);
}
}
3 編寫hellotag.tld
這是tag庫的描述部分:
<?xml version="1.0" encoding="utf-8" ?>
<!doctype taglib
public "-//sun microsystems, inc.//dtd jsp tag library 1.2//en"
"http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">
<taglib>
<tlib-version>1.0</tlib-version>
<jsp-version>1.2</jsp-version>
<shorttag -name>hellotag</short -name>
<uri>/web-inftagshellotag</uri>
<display-name>hellotag</display-name>
<small-icon></small-icon>
<large-icon></large-icon>
<description>simple hello tags tag
</description>
<tag>
<name>hellotag</name>
<tag-class >tags.hellotag</tag-class >
<body-content>empty</body-content>
<small-icon></small-icon>
<large-icon></large-icon>
<description></description>
<example></example>
</tag>
</taglib>
4 注意:
通常手工編寫xml文件,但是sun的教程建議使用ide工具編寫自定義tag,比如netbeans
一般,直接把tld文件放到web-inf目錄中。
新聞熱點
疑難解答