必须声明 web-app 元素(Servlet 4.0)

shu*_*hui 4 web.xml servlets servlet-4 jakarta-ee

我尝试使用 Servlet 4.0 命名空间。该应用程序运行良好,但 IDEA 检测到错误:“Element 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>
Run Code Online (Sandbox Code Playgroud)

快照 (我无法直接发布图像。)

当我将版本更改为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">
Run Code Online (Sandbox Code Playgroud)

根据Java EE:Java EE 部署描述符的 XML 模式# 使用 Java EE 模式

所有 Java EE 7 和更高版本的部署描述符模式共享命名空间http://xmlns.jcp.org/xml/ns/javaee/。每个模式文档都包含一个版本属性,其中包含规范的版本。例如,Servlet 规范的 XML 模式文档包含版本属性值“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">
Run Code Online (Sandbox Code Playgroud)

小智 5

我参加聚会迟到了,但我有一个可能的解决方案,但尚未给出。从我上面读到的内容来看,问题可能是 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">
Run Code Online (Sandbox Code Playgroud)