【问题标题】:Spring MVC HTTP 404 errorSpring MVC HTTP 404 错误
【发布时间】:2026-02-24 03:30:01
【问题描述】:

我学习了 SpringMVC,所以我从 HERE 关注 Spring 3.0 MVC 系列。

如你所见,我完成了第 1 部分和第 2 部分,现在我正在第 3 部分学习如何使用 Spring 3 MVC 处理表单。

但是当我尝试运行我的应用程序时,我收到了这个 HTTP 404 错误。项目结构和此错误您可以在下图中看到。

我该如何解决这个问题?

ContactController.java 代码:

package net.viralpatel.spring3.controller;

import net.virtalpatel.spring3.form.Contact;

import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

@Controller
@SessionAttributes
public class ContactController {

@RequestMapping(value = "/addContact", method = RequestMethod.POST)
public String addContact(@ModelAttribute("contact")
Contact contact, BindingResult result) {

    System.out.println("First Name:" + contact.getFirstname() + 
                "Last Name:" + contact.getLastname());

    return "redirect:contacts.html";
}

@RequestMapping("/contacts")
public ModelAndView showContacts() {

    return new ModelAndView("contact", "command", new Contact());
}}

spring-servlet.xml 代码:

<?xml version="1.0" encoding="UTF-8"?>

http://www.springframework.org/schema/beans/spring-beans-3.0.xsd


http://www.springframework.org/schema/context


http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<context:component-scan
    base-package="net.viralpatel.spring3.controller" />

<bean id="viewResolver"
    class="org.springframework.web.servlet.view.UrlBasedViewResolver">
    <property name="viewClass"
        value="org.springframework.web.servlet.view.JstlView" />
    <property name="prefix" value="/WEB-INF/jsp/" />
    <property name="suffix" value=".jsp" />
</bean>

****index.jsp 代码:****

<jsp:forward page="contacts.html"></jsp:forward>

web.xml 代码:

<?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"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee                     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>Spring3MVC</display-name>
<welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
</welcome-file-list>

<servlet>
    <servlet-name>spring</servlet-name>
    <servlet-class>
        org.springframework.web.servlet.DispatcherServlet
    </servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>spring</servlet-name>
    <url-pattern>*.html</url-pattern>
</servlet-mapping>

【问题讨论】:

标签: spring jakarta-ee servlets spring-mvc


【解决方案1】:

只需将contact 更改为contacts

改变

return new ModelAndView("contact", "command", new Contact());

return new ModelAndView("contacts", "command", new Contact());

问题出在您的转发中,它会检查contact.jsp,但实际上您有contacts.jsp(您的后缀属性为.jsp)

【讨论】:

  • 令人难以置信的错误。谢谢:)
  • 嗨@Krisl,我遇到了同样的情况,你能帮我*.com/questions/17697899/…
  • @Rey,我想你已经找到答案并解决了问题
【解决方案2】:

localhost:8080/Spring3MVC/index.jsp 如你所见,我尝试先打开 index.jsp 然后重定向到 contact.jsp - Zookey 44 分钟前

我认为你搞混了。 1)有一个错字,你说contact.jsp但文件名是contacts.jsp(eclipse中的文件名) 2)contacts.html 文件在哪里? 我建议,您首先返回 jsp,看看是否可以让控制器返回 jsp,然后尝试在创建后重定向到 html 文件。

【讨论】:

  • 这是contacts.jsp。我没有contacts.html 文件,我只有contacts.jsp。
【解决方案3】:

您的index.jsp 被转发到contacts.html

但是你的spring配置没有/contacts.html的映射,你已经映射了/contacts

您需要将/contacts 映射更改为

@RequestMapping("/contacts.html")
public ModelAndView showContacts() {
    return new ModelAndView("contact", "command", new Contact());
}

【讨论】:

  • 我改变了它,但我仍然得到这个错误。从图中可以看出:dodaj.rs/f/B/q7/3F9RnEc/error2.png
  • 它要去/contacts.jsp,你可以试试这个网址/Spring3MVC/contacts.html
  • 当我尝试 /Spring3MVC/contacts.html 时,我得到与图像相同的错误。
最近更新 更多