【问题标题】:Error in Eclipse web.xml on empty project空项目上的 Eclipse web.xml 中的错误
【发布时间】:2021-12-17 06:21:57
【问题描述】:

谁能帮帮我? 我正在使用 Apache Tomacat 10 在 Eclipse 2021-06 上创建一个新的动态 Web 项目。 我总是在 web.xml 中收到错误

<?xml version="1.0" encoding="UTF-8"?>
<web-app    id="WebApp_ID" version="2.5" 
            xmlns="http://java.sun.com/xml/ns/j2ee" 
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
            xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    <display-name>01Test</display-name>
    <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>

标记:

  • version="2.5

我收到以下错误

我该如何解决???我尝试下载Tomcat表单网站并通过Eclipse...使用Web模块4和5...但仍然有这个问题...

【问题讨论】:

  • 请在生成的部署描述符文件中打开此错误的错误报告。
  • 你从哪里得到web-app 元素中的id 元素?我在任何地方都找不到对此的参考,而且我不相信它是有效的。

标签: eclipse tomcat servlets tomcat10


【解决方案1】:

Eclipse 根据提供的架构验证 web.xml 文件:

http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd

该架构要求version 属性为2.4。如果您将其设置为正确的值2.4,错误将消失。 Servlet 2.4 也没有 &lt;display-name&gt; 标记,因此第二个 "error"

由于 Tomcat 5.5(支持 Servlet 2.4)早已不复存在,您应该将 web.xml 的架构更改为:

  • Servlet 3.1,对应于未达到生命周期结束的最旧版本的 Tomcat (Tomcat 8.5):

    <web-app
        xmlns="http://xmlns.jcp.org/xml/ns/javaee"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
        version="3.1">
    
  • Servlet 4.0,对应于 Java EE 名称下的最后一个版本:

    <web-app
        xmlns="http://xmlns.jcp.org/xml/ns/javaee"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
        version="4.0">
    
  • Servlet 5.0,对应Tomcat 10:

    <web-app
        xmlns="https://jakarta.ee/xml/ns/jakartaee"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/web-app_5_0.xsd"
        version="5.0">
    

并相应地设置“动态 Web 模块” 方面的版本。这样,Eclipse 将为您提供有关这些规范中引入的元素的完成建议。

备注:也许从那以后已经更正了,但是 Eclipse 2021-03 在 Tomcat 10 和 Jakarta EE 9 的支持方面仍然存在一些怪癖。它不允许在 Tomcat 10 上部署 Servlet 5.0 项目,它仅适用于 Servlet 4.0 及更早版本的项目。

您还需要注意,由于从 javax.* 迁移到 jakarta.* 命名空间,您的项目将无法在旧版本的 Tomcat 上运行。

【讨论】:

  • 如果您看到 2021-06 年的怪癖,请打开它们的错误报告。
  • @nitind:抱歉,这当然是正确的做法。但是只是绕过这些错误而不是帮助修复它们的诱惑很大。 :-)
猜你喜欢
  • 2023-03-09
  • 2011-06-12
  • 2018-06-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多