DmitMedv
DmitMedv

Reputation: 1010

Spring MVC. HTTP Status 404

I began to learn "Spring MVC" from this course: http://www.pluralsight.com/training/Courses/TableOfContents/springmvc-intro At step "Building->Run the Application" I'm stuck.

When I try to go to link http://localhost:8080/FitnessTracker/greeting.html I get "HTTP Status 404"

HTTP Status 404 - /FitnessTracker/WEB-INF/jsp/Hello.jsp
type Status report
message /FitnessTracker/WEB-INF/jsp/Hello.jsp
description The requested resource is not available.
Apache Tomcat/7.0.50

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    version="2.5">
  <servlet>
    <servlet-name>fitTrackerServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>/WEB-INF/config/servlet-config.xml</param-value>
    </init-param>
  </servlet>

  <servlet-mapping>
    <servlet-name>fitTrackerServlet</servlet-name>
    <url-pattern>*.html</url-pattern>
  </servlet-mapping>

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

servlet-config.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"
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
        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-3.2.xsd">

  <mvc:annotation-driven />
  <context:component-scan base-package="com.pluralsight.controller"></context:component-scan>

  <!--
  <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/jsp/"/>
    <property name="suffix" value=".jsp"/>
  </bean>
  -->

  <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
  p:prefix="/WEB-INF/jsp/" p:suffix=".jsp"/>

</beans>

HelloController.java

package com.pluralsight.controller;

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

@Controller
public class HelloController {

    @RequestMapping(value = "/greeting")
    public String sayHello (Model model) {
        model.addAttribute("greeting", "Hello WorldX");
        return "Hello"; 
    }
}

hello.jsp

<%@ page language="java" contentType="text/html; charset=US-ASCII"
    pageEncoding="US-ASCII"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>Insert title here</title>
</head>
<body>
  <h1>${greeting}</h1>
</body>
</html>

enter image description here

http://localhost:8080/FitnessTracker/ - it's work (file: webapp/index.jsp)

Upvotes: 2

Views: 36720

Answers (4)

Karan Kamboj
Karan Kamboj

Reputation: 119

When you hit http://localhost:8080/FitnessTracker/ , it search for index.jsp file and return it. But when you hit http://localhost:8080/FitnessTracker/greeting it will search for file named "Hello.jp" because you are returning Hello in the controller method. But the file name is "hello.jsp". Note that it is case sensitive. So that was your mistake. You have to retun hello rather than Hello. It should work fine after that.

@Controller
public class HelloController {

@RequestMapping(value = "/greeting")
public String sayHello (Model model) {
    model.addAttribute("greeting", "Hello WorldX");
    //return "Hello"; 
    return "hello"; 
}
}

Upvotes: 0

Tunde Pizzle
Tunde Pizzle

Reputation: 827

Change in your web.xml the to

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

Upvotes: 0

JB Nizet
JB Nizet

Reputation: 691635

The error message says that it's looking for Hello.jsp. And indeed, your controller returns the following view name: "Hello". But your screenshot shows that your file is naled hello.jsp. The case matters.

Upvotes: 7

Kevin Bowersox
Kevin Bowersox

Reputation: 94429

Try returning the properly cased view name which matches the JSP hello.jsp from the controller. Currently the controller returns Hello while the JSP is named hello.jsp, causing the 404.

@Controller
public class HelloController {

    @RequestMapping(value = "/greeting")
    public String sayHello (Model model) {
        model.addAttribute("greeting", "Hello WorldX");
        //return "Hello"; 
        return "hello"; 
    }
}

Upvotes: 4

Related Questions