自定义标签库时,在web.xml中无法配置<taglib>的问题
原因在于:
servlet 2.3的配置不需要<jsp-config>标签的,
servlet 2.4容器中<taglib>需要在<jsp-config>中。
(实际上,区分就在web.xml一开时的<web-app version="2.5"...web-app_2_5.xsd里,这里的2.5就代表2.5的版本)
完整的例子是孙卫琴的《Tomcat与Java Web开发技术详解》中的第二章:创建和发布web应用中,2.2.7 部署JSP Tag Library。
在web应用中可以加入Tag Library(标签库)。Tag Library向用户提供了自定义JSP标签的功能。以下一个例子就是自定义了一个标签库mytaglib。将<mm:hello/>解析为"hello"。
(1)编写处理hello标签的类HelloTag.java:
(2) 创建Tag Libraray的描述文件mytaglib.tld文件,在这个文件中定义mytaglib标签库和hello标签。这个文件存放的位置为/WEB-INF/mytaglib.tld。
(3)在web.xml文件中加入<taglib>元素:这里会出错,说不能识别<taglib>原因一开始已经讲了。
<taglib>中包含两个属性<taglib-uri>和<taglib-location>。<taglib-uri>指定Tag Library标示符;<taglib-location>指定Tag Library的描述文件tld的位置。
(4)在web应用中加入hello标签(hello.jsp)
<%@ tablib prefix="mm" uri="/mytaglib"%>------这句必须,并且prefix的参数mm是随便取的。
网页结果为:hello:lin!
原因在于:
servlet 2.3的配置不需要<jsp-config>标签的,
servlet 2.4容器中<taglib>需要在<jsp-config>中。
(实际上,区分就在web.xml一开时的<web-app version="2.5"...web-app_2_5.xsd里,这里的2.5就代表2.5的版本)
完整的例子是孙卫琴的《Tomcat与Java Web开发技术详解》中的第二章:创建和发布web应用中,2.2.7 部署JSP Tag Library。
在web应用中可以加入Tag Library(标签库)。Tag Library向用户提供了自定义JSP标签的功能。以下一个例子就是自定义了一个标签库mytaglib。将<mm:hello/>解析为"hello"。
(1)编写处理hello标签的类HelloTag.java:
- package mypack;
- import java.io.PrintWriter;
- import javax.servlet.jsp.JspException;
- import javax.servlet.jsp.JspTagException;
- import javax.servlet.jsp.tagext.TagSupport;
- public class HelloTag extends TagSupport {
- public void HelloTag()
- { }
- //Method called when the closing hello tag is encountered
- public int doEndTag()throws JspException
- {
- try
- {
- pageContext.getOut().print("Hello");
- }catch(Exception e)
- {
- throw new JspTagException(e.getMessage());
- }
- //this tag does not support a Tag Body(仅仅是<mm:hello/>,无标签体),so we return SKIP_BODY
- return SKIP_BODY;
- }
- public void release()
- {
- //Call the parent's release to release any resources used by the parent tag.This is just good practice for when you start creating hierarchies of tags
- super.release();
- }
- }
(2) 创建Tag Libraray的描述文件mytaglib.tld文件,在这个文件中定义mytaglib标签库和hello标签。这个文件存放的位置为/WEB-INF/mytaglib.tld。
- <?xml version="1.0" encoding="utf-8"?>
- <taglib>
- <tlibversion>1.0</tlibversion>
- <jspversion>1.1</jspversion>
- <shortname>mytaglib</shortname>
- <uri>/mytaglib</uri>
- <tag>
- <name>hello</name>
- <tagclass>mypack.HelloTag</tagclass>
- <bodycontent>empty</bodycontent>
- <info>Just Says Hello</info>
- </tag>
- </taglib>
(3)在web.xml文件中加入<taglib>元素:这里会出错,说不能识别<taglib>原因一开始已经讲了。
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app version="2.5"
- xmlns="http://java.sun.com/xml/ns/javaee"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
- http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <jsp-config>-------------------
- 如果是servlet 2.4,这个标签要加,否则不加也对
- <taglib>
- <taglib-uri>/mytaglib</taglib-uri>
- <taglib-location>/WEB-INF/mytaglib.tld</taglib-location>
- </taglib>
- </jsp-config>
- </web-app>
<taglib>中包含两个属性<taglib-uri>和<taglib-location>。<taglib-uri>指定Tag Library标示符;<taglib-location>指定Tag Library的描述文件tld的位置。
(4)在web应用中加入hello标签(hello.jsp)
<%@ tablib prefix="mm" uri="/mytaglib"%>------这句必须,并且prefix的参数mm是随便取的。
- <html>
- <head><title>helloapp</title></head>
- <body>
- <b><mm:hello/>:lin!</b>
- </body>
- </html>