Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
  • Spring 3 MVC configuration.

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 476
    Comment on it

    Introduction to Spring 3 MVC Framework

    The Spring web MVC framework provides model-view-controller architecture and ready components that can be used to develop flexible and loosely coupled web applications. The MVC pattern allow us in separating the different aspects of the application like, view logic, model/business logic and controller logic while providing a loose coupling between these elements.


         The Model consist of service and DAO layer of application and in general they will provide data to application in the form of POJO(simple java bean class).


         View is responsible for rendering the model data in the form of HTML output that the client's browser can interpret.


    The Controller is responsible for processing user requests, calling appropriate request handler method and building appropriate model and passes it to the view for rendering.

    Request Processing Lifecycle

    alt text

    Image source: Springsource

    The DispatcherServlet

    The Spring Web model-view-controller (MVC) framework is designed around a DispatcherServlet that handles all the HTTP requests and responses. The request processing flow of the Spring Web MVC DispatcherServlet is illustrated in the above diagram.

    Following is the sequence of actions happens to an incoming HTTP request to DispatcherServlet:

    1. After receiving an HTTP request, DispatcherServlet consults the DispatcherServlet to call the appropriate Controller.

    2. The Controller takes the request and calls the appropriate service/DAO methods based on used GET or POST method. The service method will set model data based on defined business logic and returns model data to controller and controller transfer this model data along with view name to DispatcherServlet.

    3. The DispatcherServlet will take help from ViewResolver to resolve the view for the request.

    4. Once view is finalized, The DispatcherServlet passes the model data to the view which is finally rendered on the browser in the form of HTML.

    Required Configuration

    We need to map requests that we want the DispatcherServlet to handle, by using a URL mapping in the web.xml file. The following is an example to show declaration and mapping for SampleWeb DispatcherServlet example:

    <web-app ...>
        <display-name>Spring MVC Application</display-name>

    The web.xml file resides in WebContent/WEB-INF directory of your web application. Upon initialization of SampleWeb DispatcherServlet, by default the framework will try to load the application context from a file named [servlet-name]-servlet.xml located in the application's WebContent/WEB-INF directory. In this case our file will be SampleWeb-servlet.xml.

    Next, tag indicates what URLs will be handled by the which DispatcherServlet. Here all the HTTP requests ending with .jsp will be handled by the HelloWeb DispatcherServlet.

    If We do not want to go with default filename as [servlet-name]-servlet.xml and default location as WebContent/WEB-INF Or we want to load other application context file also upon deployment of web application, we can customize this file name and location by adding the servlet listener ContextLoaderListener in your web.xml file as follows:


    Now, let us examine the required configuration for SampleWeb-servlet.xml file, placed in your web application's WebContent/WEB-INF directory:

    <beans xmlns=""
        xmlns:xsi="" xmlns:p=""
        <context:property-placeholder location="classpath*:properties/*.properties" />
        <context:component-scan base-package="fully qualified package path" />      
           <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
                  <property name="prefix" value="/WEB-INF/jsp/" />
                  <property name="suffix" value=".jsp" />

    Following are the important points about SampleWeb-servlet.xml file:

    1. <context:property-placeholder...> tag will be use to load property from the specified location.

    2. <context:component-scan...> tag will allow Spring to load all the components from specified package and all its child packages.

    3. The InternalResourceViewResolver will have rules defined to resolve the view names. By above defined rule, a logical view named sample is delegated to a view implementation located at /WEB-INF/jsp/sample.jsp .

 0 Comment(s)

Sign In

Sign up using

Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: