关于雅加达EE:Spring MVC HTTP 404错误

Spring MVC HTTP 404 error

我正在学习SpringMVC,因此我从这里开始关注Spring 3.0 MVC系列。

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

但是,当我尝试运行我的应用程序时,出现了HTTP 404错误。项目strucutre和此错误,您可以在下图看到。

我该如何解决?

enter

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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代码:

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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代码:****

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

web.xml代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?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>


只需将contact更改为contacts

更改

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

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

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


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

但是您的spring配置没有/contacts.html的映射,而是映射了/contacts

您需要将/contacts映射更改为

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


localhost:8080/Spring3MVC/index.jsp As you can see, I try first to open index.jsp and then redirect to contact.jsp a€" Zookey 44 mins ago

我想你把它弄混了。 1)有一个拼写错误,您说的是contact.jsp,但是文件名是contacts.jsp(eclipse中的文件名)
2)contacts.html文件在哪里?
我建议您先返回到jsp,看看是否可以让您的控制器在创建完之后尝试将其重定向到html文件后返回jsp。