将你的xxx.tld文件放入
%TOMCAT%webapps/tgglib/
目录下,然后修改
%TOMCAT%webapps/tgglib/WEB-INF/web.xml
文件,加入
<taglib>
<taglib-uri>http://www.mylib.com/testlib</taglib-uri>
<taglib-location>/xxx.tld</taglib-location>
</taglib>
在jsp文件里申明
<%@ page language="java" contentType="text/html;charset=gb2312"%>
<%@ taglib uri="http://www.mylib.com/testlib" prefix="test"%>
如下试分别调用xxx.tld里定义的两个标记类
<test:readInterestType />
<test:calcInterest yearName="" principalName="" interestName="" balanceName="">
taglib content!
</test:calcInterest>
%TOMCAT%webapps/tgglib/
目录下,然后修改
%TOMCAT%webapps/tgglib/WEB-INF/web.xml
文件,加入
<taglib>
<taglib-uri>http://www.mylib.com/testlib</taglib-uri>
<taglib-location>/xxx.tld</taglib-location>
</taglib>
在jsp文件里申明
<%@ page language="java" contentType="text/html;charset=gb2312"%>
<%@ taglib uri="http://www.mylib.com/testlib" prefix="test"%>
如下试分别调用xxx.tld里定义的两个标记类
<test:readInterestType />
<test:calcInterest yearName="" principalName="" interestName="" balanceName="">
taglib content!
</test:calcInterest>
楼上的老兄这个uri是怎么回事啊,能不能给讲解一下,我刚才给你发了短信一直都在等你的消息呢,大概你老兄休息了吧,谁能告诉我uri具体怎么写?还有个问题是,我的html页面调用本来是没问题的,但如果修改成用taglib实现就出现找不到html文件的错误,到底是怎么回事,难道跟web.xml中布置标签关系紧密,谁能告诉我是对还是错!!!
taglib map to see if a mapping exists for the uri attribute of the taglib directive:
• If the value of the uri attribute matches any of the <taglib-uri> entries,
the engine uses the value of the corresponding <taglib-location> to
locate the actual TLD file.
• If the <taglib-location> value is a root-relative URI (that is, it starts
with a /), the JSP engine assumes the location to be relative to the web
application’s document root directory. Thus, the location for the URI
http://www.manning.com/studyKit in listing 15.1 will resolve to
the TLD file <doc-root>/myLibs/studyKit.tld.
• If the <taglib-location> value is a non-root relative URI (that is, it
starts without a /), the JSP engine prepends /WEB-INF/ to the URI and
assumes the location to be relative to the web application’s document root
directory. Thus, the location for the URI http://www.manning.com/
sample.jar in listing 15.1 will resolve to the TLD file <doc-root>/
WEB-INF/yourLibs/studyKit.tld.
• If the value of the uri attribute of the taglib directive does not match any of
the <taglib-uri> entries, then the following three possibilities arise:
• If the specified uri attribute is an absolute URI, then it is an error and is
reported at translation time.
• If the specified uri attribute is a root-relative URI, it is assumed to be relative
to the web application’s document root directory.
• If the specified uri attribute is a non-root-relative URI, it is assumed to be
relative to the current JSP page. Thus, if the JSP file <doc-root>/jsp/
test.jsp contains the directive <%@ taglib prefix="test"
uri="sample.tld" %>, the engine will expect to find the sample.tld
file at <doc-root>/jsp/sample.tld.
// 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>
tagtest
│ datetag.jsp
│
└─WEB-INF
│ web.xml
│
├─classes
│ └─com
│ └─liad
│ └─jsptaglibs
│ └─date
│ DateTag.class
│
└─tlds
simple.tld
我按你的例子做下来,运行是出现以下错误,请问是什么问题
org.apache.jasper.JasperException: Unable to initialize TldLocationsCache: zip file closed