element web-app must be declared(Servlet 4.0)

16,513

Solution 1

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.

Solution 2

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

Solution 3

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

Share:
16,513
Admin
Author by

Admin

Updated on June 04, 2022

Comments

  • Admin
    Admin almost 2 years

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