• Post Reply Bookmark Topic Watch Topic
  • New Topic

Problem with DispatcherServlet mapping  RSS feed

Andrea Nobili
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am studing the Spring MVC showcase dowloaded from STS dashboard and I am finding some difficulties with mapping an url to a controller method.

At the moment I am refering only at the "Simple" tab of the Spring MVC Showcases.

In the div having id="simple" of home.jsp page I have this link:

When I click on this link an HTTP Request towards /spring-mvc-showcase/simple resources is generated, this HTTP request is managed from simple() method of org.springframework.samples.mvc.simple.SimpleController:

Ok, this is very clear for me, as you can see this controller manage the http request towards "/simple" using the annotation @RequestMapping("/simple")

So I have try to add another link, this one:
<a href="hello.html">Say Hello</a>

So, now I am generating an HTTP request towards a specific hello.html file and not towards a folder as in the previus example

Now (as I did in this other "Hello World" example: http://viralpatel.net/blogs/spring-3-mvc-create-hello-world-application-spring-3-mvc/) I have created an other controller class int the same package, the HelloController class, this one:

As you can see in this class I have used the annotation @RequestMapping("/hello") that as specified in the tutorial: The @RequestMapping annotation tells Spring that this Controller should process all requests beginning with /hello in the URL path. That includes /hello/* and /hello.html.

So I thought that Spring could map the hello.html request with the sayHello() method but it don't work....if I execute the prroject and I click on the "Say Hello" link, it don't work and give me the followong error message: HTTP Status 404, The requested resource is not available.

And in the stacktrace I have the following line:

12:15:16 [tomcat-http--26] DispatcherServlet - DispatcherServlet with name 'appServlet' processing GET request for [/spring-mvc-showcase/hello.html]
12:15:16 [tomcat-http--26] RequestMappingHandlerMapping - Looking up handler method for path /hello.html
12:15:16 [tomcat-http--26] RequestMappingHandlerMapping - Did not find handler method for [/hello.html]
12:15:16 [tomcat-http--26] PageNotFound - No mapping found for HTTP request with URI [/spring-mvc-showcase/hello.html] in DispatcherServlet with name 'appServlet'
12:15:16 [tomcat-http--26] DispatcherServlet - Successfully completed request

So...seem that my DispatcherServlet named appServlet can not manage the HTTP Request towards /spring-mvc-showcase/hello.html because don't file an handerl method for path /hello.html

But why? In the linked tutorial (that I have tryied and work well) use the annotation @RequestMapping("/hello") to mapp this path...

The only difference I noticed is that in the web.xml configuration file of the MVC Showcases project I have that my DispatcherServlet map request towards "/" pattern:

And in the other "Hello World" example, in web.xml configuration file I have that map DispatcherServlet with url pattern *.html

depends on it? or what?

Perhaps I did not understand what exactly is mapped by <url-pattern>/</url-pattern>

Thank you very much

Bill Gorder
Posts: 1682
Android IntelliJ IDE Linux Mac OS X Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you using Spring 3.1.1?

I can map it the same

Assume my controller looks like this

now assume that my contenxt root is mvc-sample

My controller will correctly respond to any of the following:

The url mapping in the web.xml decides what requests should be handled by the dispatcher servlet in the case of / all of them should. The @RequestMapping value is responsible for taking the requests the dispatcher servlet receives and mapping them to controllers.

So when you have this

The dispatcher servlet will only handle requests ending in .html also in your handler mappings you must also specify .html

now take this for example so I have in my web.xml

if I want the same hello controller to handle it I would change it from /hello in my hander mapping to / . This is because the RequestMapping annotation is matched against the HttpServletRequest.getPathInfo(), which ommits the context-path-part of the request URI. (note that this behaviour can be changed but that is how it works by default.)
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!