Spring MVC:HTTP 状态 404 – 未找到

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

我正在开发一个 Spring MVC 项目,在该项目中我设置了一个简单的控制器,其中包含两个请求映射来导航到 JSP 页面。但是,当我单击 index.jsp 中的链接时,遇到 404 错误。我已经仔细检查了我的代码和配置,但似乎找不到问题。

这是我的项目结构和代码的概述:

项目结构:

index.jsp 位于我的 Web 应用程序的根目录中。 add.jsp 和 addFinal.jsp 位于我的项目中的 /WEB-INF/jsp/ 中。

addController(addController.java):

package com.mvc;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class addController {
    @RequestMapping("/firstAdd")
    public String add(){
        return "add.jsp";
    }
    
    @RequestMapping("/secondFinal")
    public String addFinal(){
        return "addFinal";
    }
}

web.xml Servlet 映射:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
         xmlns="http://java.sun.com/xml/ns/javaee" 
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_1.xsd" 
         id="WebApp_ID" version="3.1">  
    
    <display-name>SpringMVC</display-name>
    <servlet> 
        <servlet-name>mvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    
    <servlet-mapping>
        <servlet-name>mvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    
</web-app>

Spring MVC 配置(mvc-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"> 

    <!-- Provide support for component scanning -->
    <context:component-scan base-package="com.mvc" />
        
    <!-- Provide support for conversion, formatting and validation -->
    <mvc:annotation-driven/>
    
    <!-- Define Spring mvc view resolver -->
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>
    
</beans>

问题:单击 index.jsp 中的任一链接时,出现 404 错误,URL 为 http://localhost:8080/spring_MVC_project/firstAdd。 Here is an image result

我已经检查了常见问题,例如拼写错误、区分大小写和 JSP 文件的位置。是否有任何我遗漏的内容或任何特定的配置问题可能导致此 404 错误?

任何指导或建议将不胜感激。谢谢!

spring spring-mvc http-status-code-404
1个回答
0
投票

是的,我已经找到了我的问题的解决方案,如果您遇到同样的问题或类似的问题,只需检查您的包裹即可

我的代码没有问题,但包有问题

文件 com.mvc.addController.java 它应该位于

src/main/java

但我错误地把它放进去了

src/main/resources

这就是它无法被扫描为@Controller的原因。”

谢谢大家

© www.soinside.com 2019 - 2024. All rights reserved.