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="" java.sun.com="" ns="" org.springframework.web.context.contextloaderlistener="" org.springframework.web.servlet.dispatcherservlet="" pre="" version=""1.0"" web-inf="" xml="" xmlns=""http://java.sun.com/xml/ns/j2ee"" xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xsi:schemalocation=""http://java.sun.com/xml/ns/j2ee">
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:
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <context:annotation-config> <context:component-scan base-package="com.java9s.springmvc.controllers"> <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> <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> </bean></bean></context:component-scan></context:annotation-config></beans>
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.