Java 为什么 <taglib> 在我的 web.xml 中给我一个问题?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3775264/
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-08-14 04:46:48  来源:igfitidea点击:

Why is <taglib> giving me a problem in my web.xml?

javajsp-tags

提问by Ankur

I have this web.xml

我有这个 web.xml

<?xml version="1.0" encoding="UTF-8"?>
  <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
   <display-name>jsp-tags</display-name>
     <taglib>
       <taglib-uri>mytags</taglib-uri>
       <taglib-location>/WEB-INF/mytaglib.tld</taglib-location>
    </taglib>
    <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
   </welcome-file-list> 
</web-app>

The is highlighted and the error the IDE gives is: "Invalid content was found starting with element ...

突出显示,IDE 给出的错误是:“发现以元素开头的无效内容......

What more do I need to do?

我还需要做什么?

采纳答案by Jaydeep Patel

Use this notation:

使用这个符号:

<jsp-config> 
        <taglib> 
               <taglib-uri>mytags</taglib-uri> 
               <taglib-location>/WEB-INF/jsp/mytaglib.tld</taglib-location> 
        </taglib> 
</jsp-config>

But I recommended to read this link. This tutorial will give you idea how to avoid declaring taglibs in web.xml in case of JSP 2.0

但我建议阅读此链接。本教程将让您了解如何在 JSP 2.0 的情况下避免在 web.xml 中声明标签库

回答by Synesso

In the XSD referenced in your XML, it states that for complex type web-appType you can only have a choice of zero to many of the following elements:

在 XML 中引用的 XSD 中,它指出对于复杂类型 web-appType,您只能选择零到以下多个元素:

<xsd:choice minOccurs="0" maxOccurs="unbounded">
  <xsd:group ref="javaee:descriptionGroup"/>
  <xsd:element name="distributable"
 type="javaee:emptyType"/>
  <xsd:element name="context-param"
 type="javaee:param-valueType">

  </xsd:element>
  <xsd:element name="filter"
 type="javaee:filterType"/>
  <xsd:element name="filter-mapping"
 type="javaee:filter-mappingType"/>
  <xsd:element name="listener"
 type="javaee:listenerType"/>
  <xsd:element name="servlet"
 type="javaee:servletType"/>
  <xsd:element name="servlet-mapping"
 type="javaee:servlet-mappingType"/>
  <xsd:element name="session-config"
 type="javaee:session-configType"/>
  <xsd:element name="mime-mapping"
 type="javaee:mime-mappingType"/>
  <xsd:element name="welcome-file-list"
 type="javaee:welcome-file-listType"/>
  <xsd:element name="error-page"
 type="javaee:error-pageType"/>
  <xsd:element name="jsp-config"
 type="javaee:jsp-configType"/>
  <xsd:element name="security-constraint"
 type="javaee:security-constraintType"/>
  <xsd:element name="login-config"
 type="javaee:login-configType"/>
  <xsd:element name="security-role"
 type="javaee:security-roleType"/>
  <xsd:group ref="javaee:jndiEnvironmentRefsGroup"/>
  <xsd:element name="message-destination"
 type="javaee:message-destinationType"/>
  <xsd:element name="locale-encoding-mapping-list"
 type="javaee:locale-encoding-mapping-listType"/>
</xsd:choice>

The taglib element is not referenced in the XSD at all.

在 XSD 中根本没有引用 taglib 元素。

From reading this linkit would seem that you do not need to declare taglibs in the web-app document. Simply having the version="2.5" attribute means you can reference tags in your JSPs.

通过阅读此链接,您似乎不需要在 web-app 文档中声明 taglibs。简单地拥有 version="2.5" 属性意味着您可以在 JSP 中引用标签。

回答by Daniel De León

If the taglib is inside of a maven dependency,just set the scope to compile.

如果 taglib 位于maven 依赖项内,只需设置要编译的范围。

<dependency>
     <groupId>org.apache.struts</groupId>
     <artifactId>struts-taglib</artifactId>
     <version>1.3.10</version>
     <scope>compile</scope>
</dependency>