Friday, July 1, 2011

SAXException: The content of element type "web-app" must match ...


Problem


I deploy a war in JBoss 6.0.0 ( idem JBoss 5.1), I have the following error :

Caused by: org.xml.sax.SAXException: The content of element type "web-app" must match "(icon?,display-name?,description?,distributable?,context-param*,filter*,filter-mapping*,listener*,servlet*,servlet-mapping*,session-config?,mime-mapping*,welcome-file-list?,error-page*,taglib*,resource-env-ref*,resource-ref*,security-constraint*,login-config?,security-role*,env-entry*,ejb-ref*,ejb-local-ref*)". @ vfs:///opt/jboss/jboss-6.0.0.Final/server/dr_jbossweb-standalone/deploy/getting-spring.war/WEB-INF/web.xml[53,11]

I check my web.xml and there is nothing abnormal :

<web-app>
  <display-name>Archetype Created Web Application</display-name>
  <context-param>
    <param-name>log4jConfigLocation</param-name>
    <param-value>classpath:META-INF/properties/log4j.properties</param-value>
  </context-param>

  <listener>
    <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
  </listener>

  <!--All spring configuration files need to be loaded in context. Although we have single file but
  with the growing configuration stuff we need to break down configuration files too-->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath*:META-INF/spring/integration-*.xml</param-value>
  </context-param>

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

  <servlet>
    <!--Spring's front controller or dispatcher which will be responsible to handle every web request
    and dispatch it to a specific web controller-->
    <servlet-name>integration</servlet-name>
    <display-name>integration</display-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>/WEB-INF/spring/integration-mvc.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>

  <!--Let every request be handled by Spring's Dispatcher servlet and it'll decide which web controller to call -->
  <!--based on @RequestMapping-->
  <servlet-mapping>
    <servlet-name>integration</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

  <welcome-file-list>
    <welcome-file>
      index.jsp
    </welcome-file>
  </welcome-file-list>
</web-app>

So where is the error ?


Solution

The solution is simple but a little strange :
You have to put :
  <!--All spring configuration files need to be loaded in context. Although we have single file but
  with the growing configuration stuff we need to break down configuration files too-->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath*:META-INF/spring/integration-*.xml</param-value>
  </context-param>

just before :

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

Cela donne le web.xml suivant :

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
  <display-name>Archetype Created Web Application</display-name>
  <context-param>
    <param-name>log4jConfigLocation</param-name>
    <param-value>classpath:META-INF/properties/log4j.properties</param-value>
  </context-param>
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath*:META-INF/spring/integration-*.xml</param-value>
  </context-param>  
  <listener>
    <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
  </listener>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

  <servlet>
    <!--Spring's front controller or dispatcher which will be responsible to handle every web request
    and dispatch it to a specific web controller-->
    <servlet-name>integration</servlet-name>
    <display-name>integration</display-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>/WEB-INF/spring/integration-mvc.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>

  <!--Let every request be handled by Spring's Dispatcher servlet and it'll decide which web controller to call -->
  <!--based on @RequestMapping-->
  <servlet-mapping>
    <servlet-name>integration</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

  <welcome-file-list>
    <welcome-file>
      index.jsp
    </welcome-file>
  </welcome-file-list>  
</web-app>