in Spring 3 MVC

Spring 3 MVC Annotations Tutorial | Spring 3 MVC Configuration

Spring 3 MVC Configuration and Sample Example

In the previous article we have seen the Introduction to Spring 3 MVC Framework. We have come to know the role of each and every component in Spring 3 MVC Framework. In this article, we are going to see the configuration details of Spring 3 MVC Framework. The first component that needs to be registered is the DispatcheServlet which is the Front controller for the Spring 3 MVC Framework and also responsible to initialize the framework related components. Given below is the web.xml configuration of DispatcherServlet.


<?xml encoding=""UTF-8"?><web-app" http:="" j2ee="""" ns="" org.springframework.web.context.contextloaderlistener="" org.springframework.web.servlet.dispatcherservlet="" pre="" version=""1.0"" web-inf="" xml="" xmlns="""" xmlns:xsi="""" xsi:schemalocation=""">


   The Spring 3 MVC framework should have a configuration file in which all the spring 3 mvc components needs to be registered. This file will be identified by the Spring 3 MVC framework with the name <dispactherServlet name>-servlet.xml. You can see that in the configuration, the DispatcherServlet name is given as 'springmvc'. The spring configuration file should then be named as springmvc-servlet.xml and should be in the WEB-INF folder of the application. There is no need to explicitly mention about the springmvc-servlet.xml as the spring framework will look for the file in the WEB-INF folder during the application startup. The url-pattern for the DispatcherServlet is given as '/' which will route all the request into the framework even if there is an extension like '.do' or '.htm' etc., Generally spring framework does not recommend to have an extension for the url but it is up to the project preferences. For those who prefer to have extensions for every URL in the project can go ahead and specify '*.*' or '*.do' etc., in the url-pattern of the servlet-mapping.

Loading the application context:

All the controllers and spring 3 MVC based components mapping is present in the springmvc-servlet.xml.

Bean wiring configuration file in Spring MVC:

The wiring of the beans belonging to different layers like Dao, Service Layer etc., can be configured in another xml called springmvc-context.xml. Please remember that there is not restriction that the file name should follow some convention. This file is actually loaded by the ContextLoaderListener and as the exact path of the file is explicitly mentioned and no need to follow specific file name conventions. The contextConfigLocation parameter should be configured with the location of the context xml file as the 'param-value' and this value is used by the ContextLoaderListener to load the bean wiring configuration. Spring 3 MVC Framework Components Configuration:



Step 1: Configuring the Package of controllers Spring 3 MVC uses annotations '@Controller' and '@RequestMapping' to mark the classes that act as the controllers. During the application start-up the spring 3 MVC framework should scan through the package which has the controllers and note the URLs.  The <context:component-scan base-package="com.java9s.springmvc.controllers"> tag should be given the package in which the controllers for the spring 3 mvc application are present.

Step 2: Configuring the Handler Mapping component A handler mapping component is needed to have the map of URLs and the methods that needs to invoked in the controllers when a request comes. There are different kinds of HandlerMapping components in the spring framework. For Spring 3 MVC the DefaultAnnotationHandlerMapping needs to be configured as the controller configuration is going to be in annotations.

<bean class="org.springframework.web. servlet.mvc.annotation.DefaultAnnotationHandlerMapping">

Step 3: Configuring the HandlerAdapter The HandlerAdapter component is responsible to invoke the handler methods in the controllers and supports to build the parameter arguments of the handler methods. The AnnotationMethodHandlerAdapter needs to be used for the spring 3 MVC.

<bean class="org.springframework.web.servlet. mvc.annotation.AnnotationMethodHandlerAdapter">

Step 4: Configuring the View Resolver: The View Resolver is the component which decides on what view needs to be build and forwarded. The convenient resolver if you are using JSP is the InternalResourceViewResolver and the view class that needs to be uses is the JstlView. The 'prefix' property value tells about the location of the jsp pages and it is recommended to have them inside the WEB-INF folder to avoid direct access by the users. The 'suffix' property is to tell the extension of the JSPs.

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

Srinivas Reddy

Srinivas Reddy

An IT - Specialist with loads of experience in Java, J2ee platforms and loves to share his experience on technology with You...

More Posts - Website

Follow Me:

Srinivas Reddy
Written By:

An IT - Specialist with loads of experience in Java, J2ee platforms and loves to share his experience on technology with You...