【发布时间】:2020-02-26 06:17:34
【问题描述】:
尽管看了好几个小时,我还是不明白为什么会出现 404 错误。
HTTP 状态 404 – 未找到 类型状态报告 信息 / 描述 源服务器没有找到目标资源的当前表示或不愿意透露存在的表示。 Apache Tomcat/9.0.24我有一个非常简单的 Spring MVC 应用程序,但我真的看不出错误是什么。请帮助我失去理智
结构:
SpringMVC演示
- 源
- 主要
- Java
- com.springmvc.demo
- HomeController.java
- com.springmvc.demo
- Java
- WEB-INF
- 查看
- 主菜单.jsp
- 查看
- spring-mvc-demo-servlet.xml
- web.xml
- 主要
我的控制器: HomeController.java
package com.springmvc.demo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class HomeController {
@RequestMapping("/")
public String showMyPage(){
return "main-menu"; // view name
}
我的观点: 主菜单.jsp
<!DOCTYPE html>
<html>
<body>
<h2> Spring MVC Demo - main-menu</h2>
</body>
</html>
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="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>/</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="com.springmvc.demo" />
<!-- Step 4: Add support for conversion, formatting and validation support -->
<mvc:annotation-driven/>
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
【问题讨论】:
-
您能添加您要导航的网址吗?
-
用tomcat运行,url为:Localhost:8081
-
如果您尝试使用
localhost:8081/Home,您会得到什么? -
你是如何将它部署到Tomcat的?另外,您是否检查了 Tomcat 日志以查看 webapp 是否启动正常?仅仅因为 Tomcat 启动正常并不意味着您的 webapp 启动成功。
-
/Home 也会出现 404 错误。
标签: java spring maven spring-mvc model-view-controller