java JSP 自定义标签:缺少 DTD/XML 模式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6134139/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 14:29:56  来源:igfitidea点击:

JSP custom tags: missing DTD/XML Schema

javajsp-tags

提问by stivlo

I've written my JSP custom tag with the following TLD:

我已经使用以下 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>
  ...

Now Eclipse Helios complains that "No grammar constraints (DTD or XML schema) detected for the document."

现在 Eclipse Helios 抱怨“没有检测到文档的语法约束(DTD 或 XML 模式)”。

I know how to disable the warning, I was instead wondering how to solve the problem by providing DTD or schema information.

我知道如何禁用警告,而是想知道如何通过提供 DTD 或模式信息来解决问题。

By the way in the above piece of XML I have:

顺便说一下,在上面的一段 XML 中,我有:

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee web-jsptaglibrary_2_1.xsd"

but it doesn't seem to help.

但它似乎没有帮助。

UPDATEon Peter's suggestion I went to check in my Window > Preferences > XML > XML Catalog and I found the following that may fit:

更新彼得的建议我去检查我的窗口 > 首选项 > XML > XML 目录,我发现以下可能适合:

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

So I tried to add the following to my tld:

所以我尝试将以下内容添加到我的 tld 中:

<?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">

Now I've the following errors:

现在我有以下错误:

Multiple annotations found at this line:

在这一行找到多个注释:

  • Attribute "xsi:schemaLocation" must be declared for element type "taglib".
  • Attribute "version" must be declared for element type "taglib".
  • Attribute "xmlns" with value "http://java.sun.com/xml/ns/javaee" must have a value of "http://java.sun.com/JSP/ TagLibraryDescriptor".

  • schema_reference.4: Failed to read schema document 'web-jsptaglibrary_2_1.xsd', because

    1) could not find the document; 2) the document could not be read; 3) the root element of the document is not .

  • The content of element type "taglib" must match "(tlib-version,jsp-version,short-name,uri?,display-name?,small-icon?,large-icon?,description?,validator?,listener*,tag+)".

  • Attribute "xmlns:xsi" must be declared for element type "taglib".
  • 必须为元素类型“taglib”声明属性“xsi:schemaLocation”。
  • 必须为元素类型“taglib”声明属性“version”。
  • 值为“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* ,标签+)”。

  • 必须为元素类型“taglib”声明属性“xmlns:xsi”。

采纳答案by Peter

If Eclipse doesn't automatically pick up the XSD from the library, you can always add it manually: Window > Preferences > XML > XML Catalog

如果 Eclipse 没有自动从库中选取 XSD,您可以随时手动添加它:Window > Preferences > XML > XML Catalog

In my case it was already present in the plugin section. It probably came with one of the Eclipse Java EE plugins.

就我而言,它已经存在于插件部分中。它可能带有 Eclipse Java EE 插件之一。

回答by stivlo

I didn't bother looking at this problem for over a month, because anyway everything was working in Tomcat. Recently I tried GlassFish and JBoss. While GlassFish 3.1.1 didn't complain, JBoss 7.0 refused to run the application, because of tld problems. In the meantime I've also updated Eclipse Helios to Indigo.

一个多月以来,我都没有理会这个问题,因为无论如何一切都在 Tomcat 中运行。最近我尝试了 GlassFish 和 JBoss。虽然 GlassFish 3.1.1 没有抱怨,但 JBoss 7.0 拒绝运行该应用程序,因为 tld 问题。与此同时,我还将 Eclipse Helios 更新为 Indigo。

I figured out that I was using DTD and Schema at the same time, which can't be good. I ditched the DTD and kept only the Schema. I had to rename a few tags, such as tlibversion to tlib-version, shortname to short-name, bodycontent to body-content, tagclass to tag-class, remove the info tag, which I substituted with XML comments. I guess it's probably a newer version of the specs, because in the example I followed they were named without hyphens.

我发现我同时使用了 DTD 和 Schema,这不太好。我放弃了 DTD,只保留了 Schema。我不得不重命名一些标签,例如 tlibversion 到 tlib-version,shortname 到 short-name,bodycontent 到 body-content,tagclass 到 tag-class,删除 info 标签,我用 XML 注释替换了它。我想这可能是规范的更新版本,因为在我遵循的示例中,它们的名称没有连字符。

After that everything worked: Eclipse didn't give any warning any more, JBoss ran the application perfectly. For reference here is the working tld:

之后一切正常: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>