给你一个最简单且完整的例子,来自《Professional JSP Tag Libraries》
// DateTag.java
package com.liad.jsptaglibs.date;import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.tagext.TagSupport;/**
* A custom tag that displays the current date.
*
* @author Simon Brown
*/
public class DateTag extends TagSupport { /**
* Called when the starting tag is encountered.
*/
public int doStartTag() throws JspException { // get the locale of the client and create an appropriate date format
Locale loc = pageContext.getRequest().getLocale();
DateFormat df = DateFormat.getDateInstance(DateFormat.MEDIUM, loc); try {
// and write out the formatted date to the page
pageContext.getOut().write(df.format(new Date()));
} catch (java.io.IOException ioe) {
throw new JspTagException(ioe.getMessage());
} // and skip evaluating the body of the tag (as there shouldn't be one)
return SKIP_PAGE;
}}simple.tld
==========
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!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>
<short-name>simple</short-name>
<description>Examples for the Simple Tags chapter</description>
<tag>
<name>date</name>
<tag-class>com.liad.jsptaglibs.date.DateTag</tag-class>
<body-content>empty</body-content>
<description>Inserts the current date into the page</description>
</tag>
</taglib>web.xml
=======
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE web-app PUBLIC '-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN' 'http://java.sun.com/dtd/web-app_2_3.dtd'><web-app>
<display-name>Professional JSP Tag Libraries</display-name>
<taglib>
<taglib-uri>/simpleTaglib</taglib-uri>
<taglib-location>/WEB-INF/tlds/simple.tld</taglib-location>
</taglib>
</web-app>datetag.jsp
===========
<html> <head>
<title>Professional JSP Tag Libraries</title>
</head> <%@ taglib uri="/simpleTaglib" prefix="simple" %> <body> <h1>The date is <simple:date/></h1> </body></html>用dos命令tree /f获得的目录结构是
tagtest
│ datetag.jsp
│
└─WEB-INF
│ web.xml
│
├─classes
│ └─com
│ └─liad
│ └─jsptaglibs
│ └─date
│ DateTag.class
│
└─tlds
simple.tld你的问题可能是没有配置好web.xml
// DateTag.java
package com.liad.jsptaglibs.date;import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.tagext.TagSupport;/**
* A custom tag that displays the current date.
*
* @author Simon Brown
*/
public class DateTag extends TagSupport { /**
* Called when the starting tag is encountered.
*/
public int doStartTag() throws JspException { // get the locale of the client and create an appropriate date format
Locale loc = pageContext.getRequest().getLocale();
DateFormat df = DateFormat.getDateInstance(DateFormat.MEDIUM, loc); try {
// and write out the formatted date to the page
pageContext.getOut().write(df.format(new Date()));
} catch (java.io.IOException ioe) {
throw new JspTagException(ioe.getMessage());
} // and skip evaluating the body of the tag (as there shouldn't be one)
return SKIP_PAGE;
}}simple.tld
==========
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!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>
<short-name>simple</short-name>
<description>Examples for the Simple Tags chapter</description>
<tag>
<name>date</name>
<tag-class>com.liad.jsptaglibs.date.DateTag</tag-class>
<body-content>empty</body-content>
<description>Inserts the current date into the page</description>
</tag>
</taglib>web.xml
=======
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE web-app PUBLIC '-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN' 'http://java.sun.com/dtd/web-app_2_3.dtd'><web-app>
<display-name>Professional JSP Tag Libraries</display-name>
<taglib>
<taglib-uri>/simpleTaglib</taglib-uri>
<taglib-location>/WEB-INF/tlds/simple.tld</taglib-location>
</taglib>
</web-app>datetag.jsp
===========
<html> <head>
<title>Professional JSP Tag Libraries</title>
</head> <%@ taglib uri="/simpleTaglib" prefix="simple" %> <body> <h1>The date is <simple:date/></h1> </body></html>用dos命令tree /f获得的目录结构是
tagtest
│ datetag.jsp
│
└─WEB-INF
│ web.xml
│
├─classes
│ └─com
│ └─liad
│ └─jsptaglibs
│ └─date
│ DateTag.class
│
└─tlds
simple.tld你的问题可能是没有配置好web.xml
ype Exception reportmessage description The server encountered an internal error () that prevented it from fulfilling this request.exception org.apache.jasper.JasperException: Unable to compile class for JSPAn error occurred at line: -1 in the jsp file: nullGenerated servlet error:
[javac] Since fork is true, ignoring compiler setting.
[javac] Compiling 1 source file
[javac] Since fork is true, ignoring compiler setting.
[javac] D:\softwares\Tomcat 4.1\work\Standalone\localhost\_\TagTest_jsp.java:80: cannot resolve symbol
[javac] symbol : class HelloTag
[javac] location: class org.apache.jsp.TagTest_jsp
[javac] HelloTag _jspx_th_test_hello_0 = (HelloTag) _jspx_tagPool_test_hello_name.get(HelloTag.class);
[javac] ^
[javac] D:\softwares\Tomcat 4.1\work\Standalone\localhost\_\TagTest_jsp.java:80: cannot resolve symbol
[javac] symbol : class HelloTag
[javac] location: class org.apache.jsp.TagTest_jsp
[javac] HelloTag _jspx_th_test_hello_0 = (HelloTag) _jspx_tagPool_test_hello_name.get(HelloTag.class);
[javac] ^
[javac] D:\softwares\Tomcat 4.1\work\Standalone\localhost\_\TagTest_jsp.java:80: cannot resolve symbol
[javac] symbol : class HelloTag
[javac] location: class org.apache.jsp.TagTest_jsp
[javac] HelloTag _jspx_th_test_hello_0 = (HelloTag) _jspx_tagPool_test_hello_name.get(HelloTag.class);
[javac] ^
[javac] 3 errors
这是为什么啊.
/Tomcat 4.1/webapps/ROOT/WEB-INF/web.xml
里加入以下定义
<taglib>
<taglib-uri>/mylib</taglib-uri>
<taglib-location>/WEB-INF/tlds/mylib.tld</taglib-location>
</taglib>
然后在jsp里就可以
<%@taglib uri="/mylib" prefix="test"%>
来使用自定义标记库了
Apache Tomcat/4.1.24
2003-10-16 4:57:50 org.apache.commons.digester.Digester error
严重: Parse Error at line 26 column 11: The content of element type "web-app" must match "(icon?,display-name?,description?,distributable?,context-param*,filter
*,filter-mapping*,listener*,servlet*,servlet-mapping*,session-config?,mime-mappi
ng*,welcome-file-list?,error-page*,taglib*,resource-env-ref*,resource-ref*,secur
ity-constraint*,login-config?,security-role*,env-entry*,ejb-ref*,ejb-local-ref*)
".
org.xml.sax.SAXParseException: The content of element type "web-app" must match
"(icon?,display-name?,description?,distributable?,context-param*,filter*,filter-
mapping*,listener*,servlet*,servlet-mapping*,session-config?,mime-mapping*,welco
me-file-list?,error-page*,taglib*,resource-env-ref*,resource-ref*,security-const
raint*,login-config?,security-role*,env-entry*,ejb-ref*,ejb-local-ref*)".
at org.apache.xerces.util.ErrorHandlerWrapper.createSAXParseException(Un
known Source)
我的web.xml内容如下:
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app> <servlet>
<servlet-name>helloworld</servlet-name>
<servlet-class>simple.HelloWorld</servlet-class>
</servlet>
<servlet>
<servlet-name>simple</servlet-name>
<servlet-class>simple.Simple</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>helloworld</servlet-name>
<url-pattern>/helloworld</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>simple</servlet-name>
<url-pattern>/simple</url-pattern>
</servlet-mapping>
<display-name>final encounter</display-name>
<taglib>
<taglib-uri>/mylib</taglib-uri>
<taglib-location>/WEB-INF/tlds/mylib.tld</taglib-location>
</taglib>
</web-app>
后来我索性把前面的那些servlet有关的全部去掉就剩下<web-app><taglib>这两组标签了,这时候tomcat是没有错了,可是我无法访问http://localhost:8080/index.jsp错误如下:
The requested resource (/index.jsp) is not available. 平时可是对的啊,这究竟是为什么呢?
<display-name>final encounter</display-name>
要紧跟在<web-app>后面<web-app>里面的元素其实是按照字母顺序排列的,详细请看http://java.sun.com/dtd/web-app_2_3.dtd
<web-app>
<display-name>final encounter</display-name>
<servlet>
<servlet-name>helloworld</servlet-name>
<servlet-class>simple.HelloWorld</servlet-class>
</servlet>
<servlet>
<servlet-name>simple</servlet-name>
<servlet-class>simple.Simple</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>helloworld</servlet-name>
<url-pattern>/helloworld</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>simple</servlet-name>
<url-pattern>/simple</url-pattern>
</servlet-mapping>
<taglib>
<taglib-uri>/mylib</taglib-uri>
<taglib-location>/WEB-INF/tlds/mylib.tld</taglib-location>
</taglib>
</web-app>
我现在是按照字母顺序了啊,怎么还是不行呢