java - Spring MVC,为什么我会收到 404 错误?

标签 java spring maven spring-mvc model-view-controller

尽管花了几个小时的时间,我还是不明白为什么会收到 404 错误。

HTTP Status 404 – Not Found
Type Status Report

Message /

Description The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.

Apache Tomcat/9.0.24

I have a very simple spring MVC applications but I seriously can't see what the error is. Please help I'm losing my mind

Structure:

SpringMVCDemo

  • src
    • main
      • java
        • com.springmvc.demo
          • HomeController.java
    • WEB-INF
      • view
        • main-menu.jsp
    • spring-mvc-demo-servlet.xml
    • web.xml

My Controller: 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>

最佳答案

在这种情况下,我的问题通过重新创建我的 Artifact SpringMVCdemo 得到解决:模块中发生了 war 。

我不完全明白一开始出了什么问题,但希望这对将来的人有帮助

关于java - Spring MVC,为什么我会收到 404 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58634009/

相关文章:

java - 防止 JSF 中的 XSS 反射 <f :param>

java - vert.x 的 Maven 坐标?

java - 在 Java 中将十六进制字符串转换为字节数组时出现错误

java - 如何在Java中调用以回调为参数的函数

java - Spring , Spring MVC : Migrate XML configuration to Java giving data source not found error

Spring Web MVC 渲染 View 时存在巨大的性能问题

java.library.path : sigar-amd64-winnt. dll已设置但未找到

java - 当incrementalCompileWarnings标志设置为true时GWT "No source code is available for type"

java - 检测代码是否在应用程序服务器 java 中运行的最佳方法

java - Spring注入(inject)和全局初始化对象