element web-app must be declared(Servlet 4.0)
Asked Answered
H

3

5

I tried to use Servlet 4.0 namespace. The application worked well, but IDEA detects an error: "Element web-app" must be declared".

<?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 (I can't post images directly.)

When I changed the version to 3.1, it worked well:

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

According to Java EE: XML Schemas for Java EE Deployment Descriptors# Using Java EE Schemas:

All Java EE 7 and newer Deployment Descriptor Schemas share the namespace http://xmlns.jcp.org/xml/ns/javaee/. Each schema document contains a version attribute that contains the version of the specification. For example, the XML Schema document for the Servlet specification contains the version attribute value "3.1", pertaining to the specific version of the specification as well as the schema document itself.

Is version="4.0" not supported? I use IntelliJ IDEA 2017.2.5 + javax.servlet-api 4.0 + Apache Tomcat v9.0.1.


Updated: I found an example of web.xml(in 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">
Hug answered 17/11, 2017 at 11:12 Comment(2)
Are you targeting an application server that handles 4.0 of the servlet spec? Right now that's only Glassfish 5.0 release in September of 2017 or Tomcat 9.Breakfast
@Breakfast Thank you for your relay. I use Tomcat v9.0.1.Hug
O
5

I'm late to the party, but I have a possible solution which was not yet given. From what I read above, the problem might be that the URLs use plan http instead of https. Some IDEs refuse to download resources over an unsecure connection.

<?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">
Ox answered 8/6, 2021 at 9:44 Comment(0)
J
3

IntelliJ is not finding the schemas because they are not available on the stated locations. Download the following schemas from here: - web-app_4_0.xsd - web-common_4_0.xsd - web-fragment_4_0.xsd Then select http://xmlns.jcp.org/xml/ns/javaee, hit Alt+Enter and select "Manually setup external resource" and select web-app_4_0.xsd in the file field.

Johanson answered 16/8, 2019 at 0:58 Comment(0)
L
1

I think that this is a known bug even in the most recent versions of intellij IDEA (although they claim to be 2017.3 ultimate being Java EE8 ready). At least the following BUG is still "unresolved": https://youtrack.jetbrains.com/issue/IDEA-182745

Lakieshalakin answered 13/1, 2018 at 15:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.