vendredi 1 juillet 2011

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



Problème


Lorsque je déploie mon war dans JBoss 6.0.0 ( idem JBoss 5.1), j'ai l'erreur suivante :

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]

Je regarde de plus mon web.xml et je ne vois rien d'anormal :

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

D'ou vient l'erreur ?


Solution

La solution est un peu étrange mais simple.
I faut mettre :
  <!--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>

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

1 commentaire:

  1. L'ordre des éléments dans le web.xml est strict. Il est donc nécessaire de suivre le pattern indiqué dans l'erreur.

    listener doit donc être déclaré après context-param.

    RépondreSupprimer