Spring MVC,为什么会出现404错误?

问题描述 投票:0回答:1

尽管经过数小时的浏览,我仍无法弄清为什么会出现404错误。

HTTP状态404 –找不到类型状态报告信息 /描述原始服务器找不到目标资源的当前表示,或者不愿意透露其存在。Apache Tomcat / 9.0.24

我有一个非常简单的spring MVC应用程序,但是我严重看不到错误是什么。请帮助我迷失方向

结构:

SpringMVCDemo

  • src
    • 主要
      • java
        • com.springmvc.demo
          • HomeController.java
    • WEB-INF
      • 查看
        • main-menu.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
}

我的观点:main-menu.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>
java spring maven spring-mvc model-view-controller
1个回答
0
投票

在这种情况下,通过重新创建从模块爆炸的构件SpringMVCdemo:war解决了我的问题。

我不完全理解一开始的错误是什么,但希望能对以后的人有所帮助


推荐问答