The Spring MVC framework allows one to configure an application in 2 different ways :
- via a XML configuration file
- via annotations
I use the Netbeans IDE (6.7) for Spring development.
When you create a Spring MVC project, the template generated is using the XML configuration (to specify the mapping between URL patterns and Controllers)
To enable annotation:
-in ${project}-servlet.xml, remove/comment out the declarations regarding the
- ControllerClassNameHandlerMapping
- SimpleUrlHandlerMapping
- ParameterizableViewController
-in the applicationContext.xml, add the following declarations:
*When declaring the <beans> root,
- add the 'context' namespace declaration: xmlns:context="http://www.springframework.org/schema/context
- Add the following to the xsi:schemaLocation:
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
*Add the below beans declarations:<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
<context:component-scan base-package="com.myapp"/>From the documentation of Spring 2.5,
DefaultAnnotationHandlerMapping is an "implementation of the
HandlerMapping
interface that maps handlers based on HTTP paths expressed through the
RequestMapping
annotation at the type or method level. "
You will have to declare in your Java classes (POJO) a @RequestMapping which will represent an URL pattern.
The component-scan will tell Spring to scan the base-package for classes that have been annotated (@Controller, @RequestMapping...)
See more info on
DefaultAnnotationHandlerMapping here