【问题标题】:element web-app must be declared(Servlet 4.0)必须声明元素 web-app (Servlet 4.0)
【发布时间】:2018-05-01 03:41:58
【问题描述】:

我尝试使用 Servlet 4.0 命名空间。应用运行良好,但 IDEA 检测到错误:“必须声明元素 web-app”

<?xml version="1.0" encoding="UTF-8"?>
<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">
    ...
    ...
</web-app>

Snapshot (我不能直接发布图片。)

当我将版本更改为 3.1 时,它运行良好:

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

根据Java EE: XML Schemas for Java EE Deployment Descriptors# Using Java EE Schemas

所有 Java EE 7 和更新的部署描述符模式共享命名空间 http://xmlns.jcp.org/xml/ns/javaee/。每个模式文档都包含一个版本属性,该属性包含规范的版本。 例如,Servlet 规范的 XML Schema 文档包含版本属性值“3.1”,与规范的特定版本以及模式文档本身有关。

不支持version="4.0" 吗?我使用 IntelliJ IDEA 2017.2.5 + javax.servlet-api 4.0 + Apache Tomcat v9.0.1。


更新:我找到了 web.xml 的示例(在apache-tomcat-9.0.1/webapps/examples/WEB-INF/web.xml):

<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"
  metadata-complete="true">

【问题讨论】:

  • 您的目标是处理 4.0 的 servlet 规范的应用程序服务器吗?目前这只是 2017 年 9 月发布的 Glassfish 5.0 或 Tomcat 9。
  • @stdunbar 感谢您的转发。我使用的是 Tomcat v9.0.1。

标签: servlets jakarta-ee web.xml servlet-4


【解决方案1】:

IntelliJ 未找到架构,因为它们在所述位置不可用。从here 下载以下架构: - web-app_4_0.xsd - web-common_4_0.xsd - web-fragment_4_0.xsd 然后选择http://xmlns.jcp.org/xml/ns/javaee,按Alt+Enter并选择“手动设置外部资源”并在文件字段中选择web-app_4_0.xsd。

【讨论】:

    【解决方案2】:

    我迟到了,但我有一个尚未给出的可能解决方案。从我上面读到的,问题可能是 URL 使用计划 http 而不是 https。一些 IDE 拒绝通过不安全的连接下载资源。

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
        xmlns="https://java.sun.com/xml/ns/javaee"
        xsi:schemaLocation="https://xmlns.jcp.org/xml/ns/javaee
                            https://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
        id="WebApp_ID"
        version="4.0">
    

    【讨论】:

      【解决方案3】:

      我认为即使在最新版本的 intellij IDEA 中,这也是一个已知错误(尽管他们声称 2017.3 终极版已准备好 Java EE8)。 至少以下BUG仍然“未解决”: https://youtrack.jetbrains.com/issue/IDEA-182745

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-07-01
        • 1970-01-01
        • 1970-01-01
        • 2023-02-15
        • 2019-11-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多