2

I know that there were dozens of similar topics, but I couldn't find solution in any of it. I want to make simple 'hello world' app using spring mvc. It gives me 404 with description: The requested resource is not available. In the console:

wrz 13, 2013 7:38:14 PM org.springframework.web.servlet.DispatcherServlet noHandlerFound
WARNING: No mapping found for HTTP request with URI [/gitsystem/] in DispatcherServlet with name 'dispatcher'

Controller.java:

package bg.glowacki.gitsystem.controller;

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

@Controller
public class Controller {

    @RequestMapping("/")
    public String printHelloWorld(Model model) {
        model.addAttribute("message", "Hello World!");

        return "helloWorld";
    }
}

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

    <context:component-scan base-package="bg.glowacki.gitsystem.controller" />
    <mvc:annotation-driven />

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

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>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

Request

GET /gitsystem/ HTTP/1.1 
Host: localhost:8080 
Connection: keep-alive 
Cache-Control: max-age=0 
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8 
User-Agent: Mozilla/5.0 (Windows NT 6.2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.66 Safari/537.36 
Accept-Encoding: gzip,deflate,sdch 
Accept-Language: pl-PL,pl;q=0.8,en-US;q=0.6,en;q=0.4
chrylis -cautiouslyoptimistic-
  • 75,269
  • 21
  • 115
  • 152
bglow
  • 61
  • 1
  • 2
  • 9
  • 2
    And what's your HTTP request look like? Is your application running as the root application? – chrylis -cautiouslyoptimistic- Sep 13 '13 at 17:54
  • GET /gitsystem/ HTTP/1.1 Host: localhost:8080 Connection: keep-alive Cache-Control: max-age=0 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 User-Agent: Mozilla/5.0 (Windows NT 6.2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.66 Safari/537.36 Accept-Encoding: gzip,deflate,sdch Accept-Language: pl-PL,pl;q=0.8,en-US;q=0.6,en;q=0.4 – bglow Sep 13 '13 at 18:01
  • 1
    Wild guess, you don't have a `helloWorld` view (ie. `/WEB-INF/pages/helloWorld.jsp`) or querying a bad URL –  Sep 13 '13 at 18:01
  • The log says the DispatcherServlet is trying to resolve `/gitsystem/`, but you just have a mapping for `/`. Have you tried just loading `http://localhost:8080/`? – chrylis -cautiouslyoptimistic- Sep 13 '13 at 18:03
  • Try `@RequestMapping` without `value`. – axtavt Sep 13 '13 at 18:06
  • RC: I have file helloWorld.jsp in this path; @chrylis http://localhost:8080/ gives also 404, but noting happens in console; axtavt: it behaves the same way – bglow Sep 13 '13 at 18:17
  • What is your application's context path? Is it /gitsystem/ or is it the ROOT application? – SoWeLie Sep 13 '13 at 18:33
  • I'm not sure but I think it's ROOT (it's my first spring app, i did it from tutorial) – bglow Sep 13 '13 at 18:55
  • @SoWeLie It's necessarily `/gitsystem` otherwise the `DispatcherServlet` would never get hit. – Sotirios Delimanolis Sep 13 '13 at 18:59
  • @SotiriosDelimanolis The error is being printed *by* the DispatcherServlet, which is seeing `/gitsystem` in the path. – chrylis -cautiouslyoptimistic- Sep 13 '13 at 19:35
  • if localhost:8080 gives 404 your tomcat isn't running or is running on a different port. – shazinltc Sep 13 '13 at 23:00
  • @chrylis The log statement prints the full URI. So unless OP is trying to hit for example `localhost:8080/gitsystem` instead of `localhost:8080/` we must assume `gitsystem` is the context path. – Sotirios Delimanolis Sep 13 '13 at 23:22

4 Answers4

2

Unless you haven't shown us the true code, this is the only explanation I can think of.

import org.springframework.stereotype.Controller;

@Controller
public class Controller {

    @RequestMapping("/")
    public String printHelloWorld(Model model) {
        model.addAttribute("message", "Hello World!");

        return "helloWorld";
    }
}

In the above code, org.springframework.stereotype.Controller is conflicting with the name of your class which is also Controller.

I don't know how you are compiling and building this application, but the class file won't end up in the classpath and therefore your component-scan won't find it and register it as a handler. Therefore you have nothing handling the path /. And you get

WARNING: No mapping found for HTTP request with URI [/gitsystem/] in DispatcherServlet with name 'dispatcher'

If you enable logging within your application, you will notice there is no bean created of type bg.glowacki.gitsystem.controller.Controller.

Rename your class to something else that doesn't have a naming conflict with any of the imports.

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
2

Change your servlet-mapping in web.xml and add a * in it

 <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/*</url-pattern>
 </servlet-mapping>
zpontikas
  • 5,445
  • 2
  • 37
  • 41
0

change the your servlet name dispatcher to any other name .because dispatcher is predefined name for spring3,spring4 versions.

example

<servlet>
    <servlet-name>ahok</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>ashok</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>
Ashok
  • 1
-1

By default, most app servers will make the URL to your servlet something like <context>/<servlet_path>/<rest_of_mapping>, where <context> is usually the WAR name. In your example code, you don't have any mapping on the Controller at all, and "/" on the method, but you somehow expect /gitsystem to make it to your handler? It doesn't work that way. Not to mention, your sample request doesn't have any Context portion, unless they WAR you are deploying is called gitsystem.war.

First, you should have SOMETHING on your Controller. Second, you should have something on your handler as well. Although you can modify the Context portion to not be the WAR name, you cannot get rid of the Context altogether, at least not that I know of. After the app server matches the Context, it will match the servlet. In your case, you mapped that to '/', which is fine. After that, the DispatchServlet takes care of the rest. At this point, the DispatchServlet is just matching the request to a Controller using its @RequestMapping, then to the handler using it's @RequestMapping. In your case, you ommited the first, and made the second nothing. I just don't think that will work.

CodeChimp
  • 8,016
  • 5
  • 41
  • 79
  • According to the documentation: »A @RequestMapping on the class level is not required. Without it, all paths are simply absolute, and not relative.« http://docs.spring.io/spring/docs/3.2.x/spring-framework-reference/html/mvc.html – Thomas Junk Sep 13 '13 at 21:46
  • `/gitsystem` is the context. The `@RequestMapping` is applied to that, so `/gitsystem/`. – Sotirios Delimanolis Sep 14 '13 at 02:11