Tag Archives: Invalid content was found starting with element

[Solved] IDEA javaweb web.xml File Error: Invalid content was found starting with element

Error reporting site restore

The header of the web.xml file is declared as follows:

<?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_3_1.xsd"
         version="3.1">

The servlet elements reporting errors are as follows:

<servlet>
    <description>Enter Dispatcher</description>
    <servlet-name>xxxMVC</servlet-name>
    <servlet-class>com.xxx.web.servlet.RequestDispatcher</servlet-class>
    <load-on-startup>1</load-on-startup>
    <init-param>
        <param-name>xxxName</param-name>
        <param-value>xxxValue</param-value>
    </init-param>
</servlet>

Or maybe this

Error prompt in <init-param>the line, given the specific contents are as follows:

cvc-complex-type.2.4.a: Invalid content was found starting with element 'init-param'. One of '{"http://xmlns.jcp.org/xml/ns/javaee":enabled, 
 "http://xmlns.jcp.org/xml/ns/javaee":async-supported, "http://xmlns.jcp.org/xml/ns/javaee":run-as, "http://xmlns.jcp.org/xml/ns/javaee":security-
 role-ref, "http://xmlns.jcp.org/xml/ns/javaee":multipart-config}' is expected.

Or the following error

Invalid content was found starting with element '{"http://xmlns.jcp.org/xml/ns/javaee":servlet-class}'. 
One of '{"http://xmlns.jcp.org/xml/ns/javaee":description, "http://xmlns.jcp.org/xml/ns/javaee":display-name, 
"http://xmlns.jcp.org/xml/ns/javaee":icon, "http://xmlns.jcp.org/xml/ns/javaee":servlet-name}' is expected.

The solution is as follows:

The init-paramelement to move the entire load-on-startupelement before, after modification, as follows:

<servlet>
    <description>Enter Dispatcher</description>
    <servlet-name>xxxMVC</servlet-name>
    <servlet-class>com.xxx.web.servlet.RequestDispatcher</servlet-class>
    <init-param>
        <param-name>xxxName</param-name>
        <param-value>xxxValue</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

Or

Error cause analysis (only for the first error):

Because it is the servletelement of error, so we try to find the corresponding xsd file, look at the error place in violation of which of the restrictions or constraints. According to the statement xml file header, we know that the corresponding xsd files are http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd, but from the web-app_3_1.xsdfile, we define the corresponding constraint did not find. However, we found the following code snippet:

<xsd:include schemaLocation="web-common_3_1.xsd"/>

So, we try to continue to web-common_3_1.xsdfind the corresponding limit or constraint, we find the following two snippets:

<xsd:element name="servlet" type="javaee:servletType"/>
<xsd:complexType name="servletType">
    ......
    <xsd:sequence>
        ......
        <xsd:element name="init-param"
                     type="javaee:param-valueType"
                     minOccurs="0"
                     maxOccurs="unbounded"/>
        <xsd:element name="load-on-startup"
                     type="javaee:load-on-startupType"
                     minOccurs="0">
        </xsd:element>
        ......
    </xsd:sequence>
    ......
</xsd:complexType>

From the above two pieces of code, we know that, for the servletelements, by <xsd:sequence>limiting the order of appearance of the child elements init-parammust appear load-on-startupbefore. At this point, we finally understand that the reason for the error is that we set the wrong order of the child elements.