1

In setting up a Spring app (packaged as a WAR and hosted via Tomcat) I'm getting a 404 and the following error when I try to visit 'localhost:8080':

4479 [http-bio-8080-exec-1] WARN  org.springframework.web.servlet.PageNotFound  - No mapping found for HTTP request with URI [/WEB-INF/pages/index.html] in DispatcherServlet with name 'spring'

There is most definitely a /WEB-INF/pages/index.html file.

Here's my web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

    <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>/*</url-pattern>
    </servlet-mapping>

</web-app> 

Additionally, here's my spring-servlet.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<beans 
    xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    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.company.app.controller" />

    <mvc:annotation-driven />

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

</beans>

And the get method from my controller:

package com.company.app.controller;

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

@Controller
public class SplashController {

    @RequestMapping(value="/", method=RequestMethod.GET)
    public String index() {
        return "index";
    }

}

So, what's wrong with my configuration?

Additionally: I'd like to include my console output from Spring's initial setup, maybe it will provide a clue or two as to what is missing:

0    [localhost-startStop-1] INFO  org.springframework.web.context.ContextLoader  - Root WebApplicationContext: initialization started
92   [localhost-startStop-1] INFO  org.springframework.web.context.support.XmlWebApplicationContext  - Refreshing Root WebApplicationContext: startup date [Sun Jan 26 16:16:57 EST 2014]; root of context hierarchy
147  [localhost-startStop-1] INFO  org.springframework.beans.factory.xml.XmlBeanDefinitionReader  - Loading XML bean definitions from ServletContext resource [/WEB-INF/spring-servlet.xml]
776  [localhost-startStop-1] INFO  org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping  - Mapped "{[/],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.String com.company.app.controller.SplashController.index()
1547 [localhost-startStop-1] INFO  org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping  - Root mapping to handler 'splashController'
1771 [localhost-startStop-1] INFO  org.springframework.web.context.ContextLoader  - Root WebApplicationContext: initialization completed in 1770 ms
Jan 26, 2014 4:16:59 PM org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring FrameworkServlet 'spring'
1809 [localhost-startStop-1] INFO  org.springframework.web.servlet.DispatcherServlet  - FrameworkServlet 'spring': initialization started
1813 [localhost-startStop-1] INFO  org.springframework.web.context.support.XmlWebApplicationContext  - Refreshing WebApplicationContext for namespace 'spring-servlet': startup date [Sun Jan 26 16:16:59 EST 2014]; parent: Root WebApplicationContext
1817 [localhost-startStop-1] INFO  org.springframework.beans.factory.xml.XmlBeanDefinitionReader  - Loading XML bean definitions from ServletContext resource [/WEB-INF/spring-servlet.xml]
1947 [localhost-startStop-1] INFO  org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping  - Mapped "{[/],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.String com.company.app.controller.SplashController.index()
2164 [localhost-startStop-1] INFO  org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping  - Root mapping to handler 'splashController'
2205 [localhost-startStop-1] INFO  org.springframework.web.servlet.DispatcherServlet  - FrameworkServlet 'spring': initialization completed in 396 ms
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
cscan
  • 3,684
  • 9
  • 45
  • 83

3 Answers3

1

For whatever reason, changing the servlet-mapping portion of my web.xml to this solved the problem:

<servlet-mapping>
    <servlet-name>spring</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>
cscan
  • 3,684
  • 9
  • 45
  • 83
0

I use Spring 3.2.3 release. Try this configuration:

Correct web.xml:

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

Correct Spring-servlet.xml:

<context:component-scan base-package="your.package.controller"/>
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>
<bean id="internalViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"
      p:viewClass="org.springframework.web.servlet.view.JstlView"
      p:prefix="/WEB-INF/jsp/"
      p:suffix=".jsp"/>
<bean class="org.springframework.web.servlet.view.BeanNameViewResolver"/>

Correct mapping annotacion:

@RequestMapping(value="/index", method=RequestMethod.GET)

And create index.jsp file.

Alexander Kuzmenko
  • 529
  • 1
  • 12
  • 32
  • I've changed my code to reflect yours - I still end up with the same 404 and the same WARN. – cscan Jan 26 '14 at 20:59
0

I also faced the same problem when a friend of mine suggest me to check for the package declaration.
After I knew for which package the definition of the controller request mapping and context:component-scan base-package to check for I saw it was not the same and after giving it the proper package name it started to work.

Check for the following snippet in your Spring config file

<context:component-scan base-package="your package name" > 
rene
  • 41,474
  • 78
  • 114
  • 152
  • I simply try to print hello world using spring mvc.its an very simple application and my example are already posted.my simple motive is for those who have making spring first application but they facing problem to get output.i don't know what you getting wrong so for decrease my vote. Better you visit my post in stack overflow which i posted today. – Manash Ranjan Dakua Aug 11 '17 at 17:51
  • The package which you define for your controller classes and that package you should mention on spring.xml file as an attribute for i.e – Manash Ranjan Dakua Aug 14 '17 at 05:43