Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

[MVC]Why in this example I have to use annotation-driven tag? Difference beetwen component-scan

 
Andrea Nobili
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Some days ago I begin to study this Spring Hello World Tutorial: http://viralpatel.net/blogs/spring-3-mvc-create-hello-world-application-spring-3-mvc/

In this tutorial Spring DispatcherServlet is configured using the spring-servlet.xml file, this one:



In this file I am using the context:component-scan tag to say that Spring have to scan my file serching the annotation, so for exemple, when in the controller class find that a method is annoted by @RequestMapping("/hello") annotation know that this method handle the HTTP Request toward the URL ending with "/hello". This is simple...

Now my doubt is related to the Spring MVC template project that I could automatically build in STS\Eclipse

When I create a new Spring MVC project in STS I have that my DispatcherServlet is configured by a file named servlet-context.xml that contains some configuration similar to the previus example file.

In this file, I still have the component scan tag:



but I have also another tag (that look like have similar task), this one:



What is the difference between these two tags? between these two tags?

An other "strange" thing is that the previus example (that don't use the annotation-driven tag) is very very similar to the project create by STS using the Spring MVC Template project but if I delete the annotation-driven tag from its configuration file the project don't run and give me the following error: HTTP Status 404 -

And in the stacktrace I have:

WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/maventestwebapp/] in DispatcherServlet with name 'appServlet'

But why? In the previus example work well without annotation-driven tag, and this controller class is very similar...infact have only one method that handles HTTP request toward "/" path

This is the code of my controller class:


Can someone help me to understand this thing?

Thank you very much

Andrea
 
Bill Gorder
Bartender
Posts: 1682
7
Android IntelliJ IDE Linux Mac OS X Spring
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic