简体   繁体   中英

How to solve web-app must be declared error in Spring?

My web.xml looks like 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" 
     xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
     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">  
<display-name>StudentCRUDWithSpringREST</display-name>  

<servlet>    
 <servlet-name>spring</servlet-name>    
 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>    
 <load-on-startup>1</load-on-startup>      
</servlet>   


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

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

</web-app>

The pom.xml file is something like this

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>StudentCRUDWithSpringREST</groupId>
<artifactId>StudentCRUDWithSpringREST</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->  
    <dependency>  
        <groupId>org.springframework</groupId>  
        <artifactId>spring-webmvc</artifactId>  
        <version>5.1.1.RELEASE</version>  
    </dependency>  
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>5.1.1.RELEASE</version>
    </dependency>  
    <!-- https://mvnrepository.com/artifact/org.apache.tomcat/tomcat-jasper -->  
    <dependency>  
        <groupId>org.apache.tomcat</groupId>  
        <artifactId>tomcat-jasper</artifactId>  
        <version>9.0.12</version>  
    </dependency>  
        <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->  
    <dependency>    
        <groupId>javax.servlet</groupId>    
        <artifactId>servlet-api</artifactId>    
        <version>3.0-alpha-1</version>    
    </dependency> 
    <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet.jsp -->
    <dependency>
        <groupId>javax.servlet.jsp</groupId>
        <artifactId>jsp-api</artifactId>
        <version>2.1</version>          
    </dependency> 
    <!-- https://mvnrepository.com/artifact/javax.servlet/jstl -->  
    <dependency>  
        <groupId>javax.servlet</groupId>  
        <artifactId>jstl</artifactId>  
        <version>1.2</version>  
    </dependency>  
        <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->  
    <dependency>  
        <groupId>mysql</groupId>  
        <artifactId>mysql-connector-java</artifactId>  
        <version>8.0.11</version>  
    </dependency>  
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->  
    <dependency>  
        <groupId>org.springframework</groupId>  
        <artifactId>spring-jdbc</artifactId>  
        <version>5.1.1.RELEASE</version>  
    </dependency>  
 </dependencies>
 <build>
 <sourceDirectory>src</sourceDirectory>
 <plugins>
   <plugin>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.5.1</version>
    <configuration>
      <source>1.8</source>
      <target>1.8</target>
    </configuration>
  </plugin>
  <plugin>
    <artifactId>maven-war-plugin</artifactId>
    <version>3.0.0</version>
    <configuration>
      <warSourceDirectory>WebContent</warSourceDirectory>
    </configuration>
  </plugin>
</plugins>
</build>
</project>

Whenever I compile the Code on Tomcat 4 I get the error

PARSE error at line 6 column 39
org.xml.sax.SAXParseException: Element type "web-app" must be declared.

My Project is a simple CRUD operation on Student DB using Spring RESTful Webservices. The pom.xml define dependencies on spring framework 5, tomcat 9 and mysql 8.
I have googled the reason behind this and mostly come across explanation that says When the order of servlet, servlet-mapping, welcome-file-list is not maintained the web-app declaration error is thrown.But that is not the scenario here.Can anyone explain What I am missing?

You haven't shared enough information. Pom is important and you use Tomcat 4 of 2006. Are you serious? The below example works, so compare and fix http://websystique.com/springmvc/spring-4-mvc-helloworld-tutorial-full-example/

web-app is part of j2ee

EDIT: As you added more info - pom. I see no issue with it.

I did update a bit though - my endpoint are / and /something...so url-pattern is not /* for me...it is /

<servlet>    
 <servlet-name>spring</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>  

<servlet-mapping>    
 <servlet-name>spring</servlet-name>    
 <url-pattern>/</url-pattern>    
</servlet-mapping>  

Please start a brand new app and build step by step

Note: Check if you can clean your repo...may be issue with a dependency

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM