5

I'm absolutely newbie with Java and Spring and I want to learn from example.

I'm using an out-of-the-box configuration / installation

  • Mac OSX
  • Springsource Tool Suite as IDE
  • Spring 2.8.1.RELEASE
  • vfabric-tc-server-developer-2.6.1.RELEASE

I've tried to generate a new project based on "Spring Template Project". Then I've chosen the "Spring MVC Project". The sample project is generated. After that, without modifying anything, I've tried to execute de "home.jsp" page via "Run As". The Web Server starts and finally I received the error in the console Tab.

No mapping found for HTTP request with URI [/myproject/] in DispatcherServlet with name 'appServlet'

And this other output in these web pages:

  • http://localhost:8080/myproject/WEB-INF/views/home.jsp
  • http://localhost:8080/myproject

enter image description here

Here you can see an image on how my project is structured (auto generated for STS):

enter image description here

What is wrong?

Here you can see the content of the web.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 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">

    <!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/root-context.xml</param-value>
    </context-param>

    <!-- Creates the Spring Container shared by all Servlets and Filters -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- Processes application requests -->
    <servlet>
        <servlet-name>appServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

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

</web-app>

The root-context.xml file has this content.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    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-3.0.xsd">

    <!-- Root Context: defines shared resources visible to all other web components -->

</beans>

And finally the servlet-context.xml has this content.

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:beans="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
        http://www.springframework.org/schema/beans 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">

    <!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->

    <!-- Enables the Spring MVC @Controller programming model -->
    <annotation-driven />

    <!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
    <resources mapping="/resources/**" location="/resources/" />

    <!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
    <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <beans:property name="prefix" value="/WEB-INF/views/" />
        <beans:property name="suffix" value=".jsp" />
    </beans:bean>

    <context:component-scan base-package="com.mycompany.myapp" />

</beans:beans>

Does anybody have an idea to solve it?

Brian Clozel
  • 46,620
  • 12
  • 129
  • 152
Marc Pou
  • 619
  • 2
  • 7
  • 18

3 Answers3

1

Spring's convention is to assume that the <servlet-name> in the web.xml for the DispatcherServlet matches the beginning of the Spring servlet context XML file. Rename servlet-context.xml to appServlet-context.xml and see if that helps.

duffymo
  • 293,097
  • 41
  • 348
  • 541
  • Trying that I got the same error. In fact, in the web.xml file, there's a line that specifies the name of the servlet... Now I've changed for the one you suggested (plus the rename of the file)... but the result is the same. contextConfigLocation /WEB-INF/spring/appServlet/appServlet-context.xml – Marc Pou Jan 13 '12 at 10:35
0

Everything under WEB-INF is not accessible from the outside, and the point of an MVC framework is to invoke a controller before dispatching to the view, so invoking the JSP directly should not be done anyway.

And you probably don't have any Spring controller mapped to the root URL, so obviously, there is nothing at the URL http://localhost:8080/myproject/.

JB Nizet
  • 633,450
  • 80
  • 1,108
  • 1,174
  • @AlfonsoDominguez @duffymo The autogenerated project has a file called "HomeController.java" with the following content: ` package com.mycompany.myapp; [...] OMITED PART [...] /** * Handles requests for the application home page. */ @Controller public class HomeController { /** * Simply selects the home view to render by returning its name. */ @RequestMapping(value = "/", method = RequestMethod.GET) public String home(Locale locale, Model model) { String str = "Server message"; model.addAttribute("serverTime", str ); return "home"; } } ` Is that what you say? – Marc Pou Jan 13 '12 at 10:48
  • yep, that's what I meant, did you try to access the URL `http://localhost:8080/myproject/home` ? That URL seems to be the correct one from your configuration files. – Alonso Dominguez Feb 02 '12 at 10:59
0

Add a Controller to your application that returns a ModelAndView instance with name "home". Then configure some handler mapping to that Controller and try to access your web app with a URL similar to this one: http://localhost:8080/myproject/home.do. More information can be found here and here.

Community
  • 1
  • 1
Alonso Dominguez
  • 7,374
  • 1
  • 24
  • 37