【发布时间】:2021-10-25 02:44:58
【问题描述】:
我是 spring mvc 和 tomcat 的新手。 我开发了一个演示 spring mvc 项目并尝试通过 eclipse 将其部署在 tomcat 9 上。 服务器成功启动,但是当我尝试从浏览器访问 url 时,我得到 404 并在屏幕上显示以下错误消息。 :
消息请求的资源[/spring-mvc-demo/]不可用
描述 源服务器没有找到目标资源的当前表示或不愿意透露存在。
以下是我的代码详细信息:
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<display-name>spring-mvc-demo</display-name>
<absolute-ordering />
<!-- Spring MVC Configs -->
<!-- Step 1: Configure Spring MVC Dispatcher Servlet -->
<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/spring-mvc-demo-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- Step 2: Set up URL mapping for Spring MVC Dispatcher Servlet -->
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/spring-mvc-demo</url-pattern>
</servlet-mapping>
</web-app>
spring-mvc-demo-servlet.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">
<!-- Step 3: Add support for component scanning -->
<context:component-scan base-package="main.webapp" />
<!-- Step 4: Add support for conversion, formatting and validation support -->
<mvc:annotation-driven/>
<!-- Step 5: Define Spring MVC view resolver -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
HomeController.java
package main.webapp.springdemo.controller;
import javax.annotation.PostConstruct;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class HomeController {
@PostConstruct
public void init() {
System.out.println("HomeController bean getting intiated");
}
@RequestMapping("/")
public String getMainMenu() {
return "main-menu";
}
}
我尝试通过 eclipse 和手动在 tomcat 9 上运行这个应用程序,但在这两种情况下我都遇到了同样的错误。
【问题讨论】:
-
你能重新确认你的servlet映射吗?
-
@VeKe servlet-mapping 在问题中是正确的。你觉得它有什么问题吗?
-
这个答案你能看一遍吗stackoverflow.com/a/11731512/1878022
-
@VeKe 我浏览了您分享的答案,发现我的战争文件在 WEB-INF 下没有 classes 文件夹。不知道为什么我的战争文件没有类文件
-
我是 spring mvc 和 tomcat 的新手。 然后丢弃你过时的教程并使用 Spring Boot。您可以完全放弃 XML 配置和 Tomcat 设置。 Spring Initializr 将为您自动生成一个可运行的项目框架。 (我强烈推荐 Thymeleaf 而不是 JSP;它解决了很多核心问题,并且仍然非常熟悉。)
标签: java spring spring-mvc tomcat