47

This is my web.xml xsd

<?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    version="3.0">

Here is servlet node

<servlet>
    <servlet-name>spring1</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
    <init-param> <!-- here is a problem -->
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring-servlet.xml</param-value>
    </init-param>
</servlet>

On the marked line xml validator says

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

What is wrong and how do I correct this error?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
michael nesterenko
  • 14,222
  • 25
  • 114
  • 182

3 Answers3

140

The order of elements in web.xml matters and in all examples I've come across, the <load-on-startup> comes after <init-param>.

<servlet>
    <servlet-name>spring1</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring-servlet.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Bala R
  • 107,317
  • 23
  • 199
  • 210
  • 3
    You would think that there is a NICE way of saying that! instead of Invalid content was found starting with element 'init-param'. Maybe a case of RTFM...then id be busy reading till the cows come home... – Gerrie May 25 '13 at 11:24
  • 2
    This is madness. It also needs to come _before_ `` – Craig Otis Jul 22 '15 at 20:23
  • @CraigOtis just changed `` place to _after_ `` and saved. After that changed back to its old place and saved. Problem is gone now. Eclipse is the best IDE ever :) **eclipse 2019-03** – omerhakanbilici Mar 27 '19 at 07:01
17

It's pedantic, but <init-param> has to come before <load-on-startup>, so:

<servlet>
    <servlet-name>spring1</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param><!--here is a problem-->
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring-servlet.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
skaffman
  • 398,947
  • 96
  • 818
  • 769
0

Configure according to this

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

      <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>

        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/dispatcher-servlet-context.xml</param-value>
        </context-param>

        <servlet>
            <servlet-name>dispatcher-servlet</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <init-param>
                <param-name>contextConfigLocation</param-name>
                <param-value></param-value>
            </init-param>
            <load-on-startup>1</load-on-startup>
        </servlet>

        <servlet-mapping>
            <servlet-name>dispatcher-servlet</servlet-name>
            <url-pattern>/*</url-pattern>
        </servlet-mapping>

    </web-app>
CannonSuyash
  • 157
  • 1
  • 2
  • 10