Zookey
Zookey

Reputation: 2707

Spring MVC HTTP 404 error

I learning SpringMVC so I am followed Spring 3.0 MVC Series from HERE.

As you can see, I completed Part1, Part2, and I am right now on Part3 where I am learning how to handle forms with Spring 3 MVC.

But I get this HTTP 404 eror, when I try to run my application. Project strucutre and this error you can see at image below.

How I can fix this?

enter image description here

ContactController.java code:

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 code:

<?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 code:****

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

web.xml code:

<?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>

Upvotes: 0

Views: 1005

Answers (3)

Kris
Kris

Reputation: 1902

Just change contact to contacts

change

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

to

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

The issue is in your forward it will check for the contact.jsp but actually you have contacts.jsp (you have suffix property as .jsp )

Upvotes: 1

happybuddha
happybuddha

Reputation: 1358

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

I think you have it mixed up. 1) There is a typo, you say contact.jsp but the file name is contacts.jsp (file name in eclipse) 2) Where is the contacts.html file ? I would suggest, you first return to the jsp and see if you can get your controller to return the jsp after that try redirecting to the html file after you create one.

Upvotes: 0

Arun P Johny
Arun P Johny

Reputation: 388416

your index.jsp is forwarded to contacts.html.

But you spring configuration does not have mapping for /contacts.html, you have mapped /contacts instead.

You need to change the /contacts mapping to

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

Upvotes: 1

Related Questions