JSP定制标记:缺少DTD / XML架构

问题描述

|| 我已经用以下TLD编写了JSP自定义标签
<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<taglib
  xsi:schemaLocation=\"http://java.sun.com/xml/ns/javaee web-jsptaglibrary_2_1.xsd\"
  xmlns=\"http://java.sun.com/xml/ns/javaee\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"
  version=\"2.1\">
  <tlibversion>1.0</tlibversion> 
  <jspversion>2.1</jspversion>
  ...
现在,Eclipse Helios抱怨“没有为文档检测到语法约束(DTD或XML模式)。” 我知道如何禁用该警告,而是想知道如何通过提供DTD或架构信息来解决问题。 顺便说一下,在上面的XML中,我有
xsi:schemaLocation=\"http://java.sun.com/xml/ns/javaee web-jsptaglibrary_2_1.xsd\"
但这似乎没有帮助。 关于Peter的建议的更新我去检查了Window> Preferences> XML> XML Catalog,发现以下内容可能适合:
Entry element:  Public
Location:   dtdsAndSchemas/web-jsptaglibrary_1_2.dtd in jar file 
usr/local/eclipse/plugins/org.eclipse.jst.standard.schemas_1.1.0.v201003031644.jar
URI:       jar:file:/usr/local/eclipse/plugins/org
  .eclipse.jst.standard.schemas_1.1.0.v201003031644.jar!/dtdsAndSchemas
  /web-jsptaglibrary_1_2.dtd
Key type:   Public ID
Key:    -//Sun Microsystems,Inc.//DTD JSP Tag Library 1.2//EN
因此,我尝试将以下内容添加到我的顶级域名:
<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<!DOCTYPE taglib PUBLIC \"-//Sun Microsystems,Inc.//DTD JSP Tag Library 1.2//EN\" 
\"http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_2.dtd\">
现在,我遇到以下错误: 在此行找到多个注释: 必须为元素类型“ taglib”声明属性“ xsi:schemaLocation”。 必须为元素类型“ taglib”声明属性“版本”。 值为\“ http://java.sun.com/xml/ns/javaee \”的属性\“ xmlns \”必须具有值为\“ http://java.sun.com/JSP/ TagLibraryDescriptor \”。 schema_reference.4:无法读取架构文档\'web-jsptaglibrary_2_1.xsd \',因为 1)找不到文件; 2)无法读取文件; 3)文档的根元素不是。 元素类型\“ taglib \”的内容必须匹配\“(tlib-version,jsp-version,short-name,uri?,display-name?,small-icon?,large-icon?,description?,validator? ,listener *,tag +)\“。 必须为元素类型“ taglib”声明属性“ xmlns:xsi”。     

解决方法

如果Eclipse不会自动从库中提取XSD,那么您始终可以手动添加它: 窗口>首选项> XML> XML目录 就我而言,它已经存在于插件部分。它可能随Eclipse Java EE插件之一一起提供。     ,我一个多月都没有理会这个问题,因为无论如何一切都在Tomcat中工作。最近,我尝试了GlassFish和JBoss。尽管GlassFish 3.1.1没有抱怨,但JBoss 7.0由于存在tld问题而拒绝运行该应用程序。同时,我还将Eclipse Helios更新为Indigo。 我发现我同时使用了DTD和Schema,这不太好。我放弃了DTD,只保留了模式。我必须重命名一些标签,例如tlibversion到tlib-version,shortname到short-name,bodycontent到body-content,tagclass到tag-class,删除info标记,我将其替换为XML注释。我想这可能是规范的更新版本,因为在我遵循的示例中,它们的名称没有连字符。 之后一切正常:Eclipse不再发出任何警告,JBoss完美地运行了该应用程序。供参考的是工作TLD:
<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<taglib
        xsi:schemaLocation=\"
            http://java.sun.com/xml/ns/javaee 
            http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd\"
        xmlns=\"http://java.sun.com/xml/ns/javaee\" 
        xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"
        version=\"2.1\">
    <tlib-version>1.0</tlib-version> 
    <short-name>StivloTags</short-name>
    <uri>http://www.stefanolocati.it/</uri>

    <!-- Example from http://www.stardeveloper.com/articles/display.html?article=2001081301&amp;page=1 -->
    <tag> 
        <name>firstTag</name> 
        <tag-class>obliquid.tag.FirstTag</tag-class>
        <body-content>empty</body-content> 
        <attribute>
            <name>name</name>
            <required>false</required>
        </attribute>
    </tag> 

    <!-- Truncate text after maxLength or 80 chars,adding \"&amp;hellip;\" if the the text was longer -->
    <tag>
        <name>ellipsis</name>
        <tag-class>obliquid.tag.Ellipsis</tag-class>
        <body-content>scriptless</body-content>
        <attribute>
             <name>maxLength</name>
             <required>false</required>
        </attribute>
    </tag>

</taglib>