jsp標簽取代了JSP中的Java程序並且可以重復使用方便不熟悉Java編程的網頁設計人員
首先在eclipse下新建一個動態web工程
在</web
app>之前加入
<jsp
config>
<taglib>
<taglib
uri>/tld/helloworld</taglib
uri>
<taglib
location>/WEB
INF/tlds/helloworld
tld</taglib
location>
</taglib>
</jsp
config>
在WEB
INF下新建tlds目錄
在tlds下新建helloworld
tld
helloworld
tld的內容為
<?xml version="
" encoding="ISO
"?>
<!DOCTYPE taglib PUBLIC "
//Sun Microsystems
Inc
//DTD JSP Tag Library
//EN"
"
<taglib>
<tlib
version>
</tlib
version>
<jsp
version>
</jsp
version>
<short
name>mytag</short
name>
<tag>
<name>helloworld</name>
<tag
class>mytag
HelloWorldTag</tag
class>
<body
content>empty</body
content>
</tag>
</taglib>
在src的mytag包下新建類HelloWorldTag
java
package mytag;
}
public int doStartTag() throws JspTagException
{
return EVAL_BODY_INCLUDE;
}
public int doEndTag() throws JspTagException
{
try
{
pageContext
getOut()
write("This is a jsp tag test!");
}catch(IOException ex)
{
throw new JspTagException("錯誤");
}
return EVAL_PAGE;
}
}
測試頁面hello
jsp
import java
io
IOException;
import javax
servlet
jsp
JspTagException;
import javax
servlet
jsp
tagext
TagSupport;
public class HelloWorldTag extends TagSupport
{ public HelloWorldTag()
<%
@ page language="java" contentType="text/html; charset=gb
"
< pageEncoding="gb
"%>
<%
@ page import="java
util
*"%>
<%
@ page import="jstl
*"%><%
@ taglib prefix="mytag" uri="/tld/helloworld"%>
<!DOCTYPE html PUBLIC "
//W
C//DTD HTML
Transitional//EN" "
<html>
<head>
<meta http
equiv="Content
Type" content="text/html; charset=gb
">
<title>Insert title here</title>
</head>
<body>
<h
>自定義標簽:</h
>
<br>
<br>
<mytag:helloworld></mytag:helloworld>
<br>
</form>
</body>
</html>
From:http://tw.wingwit.com/Article/program/Java/JSP/201311/20179.html