у меня возникла проблема с развертыванием Spring MVC приложения на Tomcat. Когда я перехожу по URL http://localhost:8080/myApp/admin, я получаю ошибку HTTP Status 404 – Не найдено.
Структура проекта:
├───.idea
├───src
│ └───main
│ ├───java
│ │ └───ru
│ │ └───my
│ │ └───app
│ │ └───HelloController.java
│ ├───resources
│ └───webapp
│ └───WEB-INF
│ └───views
│ └───admin.html
└───target
├───classes
│ └───ru
│ └───my
│ └───app
└───generated-sources
└───annotations
Контроллер:
package ru.my.app;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class HelloController {
@GetMapping("/admin")
public String adminPanel() {
return "admin";
}
}
pom.xml
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.my.app</groupId>
<artifactId>myApp</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>myApp Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<spring.version>6.1.11</spring.version>
</properties>
<dependencies>
<!-- Spring and Thymeleaf dependencies -->
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring5</artifactId>
<version>3.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>myApp</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.13.0</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.4.0</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</build>
</project>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="myApp" version="3.1">
<display-name>myApp</display-name>
<absolute-ordering/>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContextMVC.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
applicationContextMVC.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="ru.my.app"/>
<mvc:annotation-driven/>
<bean id="templateResolver" class="org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver">
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".html"/>
</bean>
<bean id="templateEngine" class="org.thymeleaf.spring5.SpringTemplateEngine">
<property name="templateResolver" ref="templateResolver"/>
<property name="enableSpringELCompiler" value="true"/>
</bean>
<bean class="org.thymeleaf.spring5.view.ThymeleafViewResolver">
<property name="templateEngine" ref="templateEngine"/>
<property name="order" value="1"/>
<property name="viewNames" value="*"/>
</bean>
</beans>
Логи-
Access:
0:0:0:0:0:0:0:1 - - [29/Jul/2024:11:53:37 +0300] “GET /myApp HTTP/1.1” 302 -
0:0:0:0:0:0:0:1 - - [29/Jul/2024:11:53:38 +0300] “GET /myApp/ HTTP/1.1” 200 48
0:0:0:0:0:0:0:1 - - [29/Jul/2024:11:53:49 +0300] “GET /myApp/admin HTTP/1.1” 404 702
0:0:0:0:0:0:0:1 - - [29/Jul/2024:11:54:05 +0300] “GET /myApp/admin HTTP/1.1” 404 702
Catalina:
29-Jul-2024 11:51:57.265 WARNING [main] org.apache.tomcat.util.descriptor.web.WebXml.setVersion Unknown version string [6.0]. Default version will be used.
Вопрос: Что может быть причиной ошибки HTTP Status 404 – Не найдено при переходе по URL http://localhost:8080/myApp/admin? Какие дополнительные шаги я могу предпринять для диагностики и исправления этой проблемы?