首页 > 解决方案 > HTTP 状态 404 - 在 spring-mvc 中使用 RequestMapping 注释未找到

问题描述

这是 index.jsp 文件

<html>
<body>
<form action="add">
    <input type="text" name="t1"></br>
    <input type="text" name="t2"></br>
    <input type="submit">
</form>
</body>
</html>

这是 todo-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">

    <context:component-scan base-package="com.telusko"></context:component-scan>
    
    <mvc:annotation-driven />
    
    <mvc:annotation-driven enable-matrix-variables="true"/>
    
   

</beans>

web.xml 文件 ->

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
    <display-name>Archetype Created Web Application</display-name>

    <servlet>
        <servlet-name>todo</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>todo</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>

AddController.java 文件

package com.telusko;

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

@Controller
public class AddController {
    @RequestMapping("/add")
    public String add() {
        return "display.jsp";
    }

}

我面临的错误

HTTP Status 404 – Not Found


Type Status Report

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/10.0.10

给出输入后,错误出现,但错误显示。我还尝试了堆栈溢出时可用的其他解决方案,但没有一个有效。我使用的是 spring mvc 4 并使用了一些 youtube 视频。

标签: javaspringspring-mvcspring-annotations

解决方案


尝试在 html 表单的 action 属性中添加“/”

<form action="/add">

推荐阅读