Spring MVC

Spring Web MVC (model-view-controller) framework provides MVC architecture for Java developers to build web applications that can take advantage of Spring framework features.


Request Lifecycle in Spring MVC

SQL MVC Request Lifecycle

All requests in Spring MVC are first intercepted by the DispatcherServlet. It acts as a 'front controller', responsible to delegate the request to other components in the web application.

Once the request reaches the DispatcherServlet it needs to identify the Controller where it should send the request for processing. Controller is a Spring component responsible for processing the request. 

Any typical web application would have multiple controllers. To decide which controller to send the request, the DispatcherServlet needs to consult the Handler Mapping to identify the appropriate controller. The Handler Mapping inspects the URL to decide the appropriate controller that should serve the request. Once the controller is decided the DispatcherServlet sends the request to it.

At the controller the request is processed and then finally it identifies a view (logical view so that the Controller is not coupled with the View implementation) that should render the output. It sends the processed request along with the view name (logical) back to the DispatcherServlet.

Now the DispatcherServlet needs to identify the actual View implementation from the logical view name. It consults the View Resolver to identify the actual View implementation and send the processed request to it.

The request’s job ends at the View implementation. It drops all its (processed) data here, the view renders the final response and sends it back to the DispatcherServlet, who finally sends the response back to the client browser.


ContextLoaderListener

A Spring web application contains two types of application context. One is the 'Application Context'; the other is the 'Web Application Context'.

'Application Context' is initialized by the ContextLoaderListener. It is the root application context and the beans declared here can be accessed throughout the application. It can be defined in the web.xml as shown below:

<listener>
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<context-param>
     <param-name>contextConfigLocation</param-name>
     <param-value>/WEB-INF/springConfig/applicationContext.xml</param-value>
</context-param>

Here we are telling Spring to create the Application Context and initialize the beans from the /WEB-INF/springConfig/applicationContext.xml file.

It is expected to contain the middle-tier and the data-tier components of the application, which is supposed to constitute the back-end of the application.

DispatcherServlet

A DispatcherServlet in Spring MVC is associated with a "Web Application Context". It is a child context of the root application context. It can be initialized in the web.xml as shown below:

<servlet>
    <servlet-name>cubearticle</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
	<param-name>contextConfigLocation</param-name>
	<param-value>/WEB-INF/webConfig/webContext.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>cubearticle</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

It is expected to contain the web components of the application like the controllers, view resolvers, handler mappings etc.


Multiple DispatcherServlets in Spring Web Application

We can have multiple DispatcherServlets configured for our application, each of which should be associated with its own “Web Application Context”.

Consider an application which is supposed to have different versions for different geographical locations. Let’s say we have two versions of the application one for Asia and the other for Europe. Here we can configure two web application context one for Asia and the other for Europe. It can be configured as shown below:

<servlet>
    <servlet-name>cubearticle_asia</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/webConfig/webContext_asia.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet>
    <servlet-name>cubearticle_europe</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/webConfig/webContext_europe.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

We can map the requests to the appropriate DispatcherServlet by using servlet mappings as shown below.

<servlet-mapping>
    <servlet-name> cubearticle_asia </servlet-name>
    <url-pattern>/asia/*</url-pattern>
</servlet-mapping>

<servlet-mapping>
    <servlet-name>cubearticle_europe</servlet-name>
    <url-pattern>/europe/*</url-pattern>
</servlet-mapping>

Thus the url http://www.cubearticle.com/asia/homePage will be serviced by cubearticle_asia and http://www.cubearticle.com/europe/homePage will be serviced by cubearticle_europe.

POPULAR ARTICLES

Creating Conditional Beans in Spring

The concept of condition beans enables Spring to restrict the creation of any bean depending on the evaluation of a condition. These beans get created only when a preset condition is evaluated as true

View Article

Accepting Request Param and Path Variable in Spring Controller

Spring MVC provides various ways through which a client browser can pass data to the Controller. In this article we will discuss about accepting Request Parameters and Path Variables in Spring Contr..

View Article

Generate Namespace & Schema Information using JAXB

Most xml documents used in enterprise applications makes use of namespace to avoid element name conflicts. This article talks about generating these namespace and schema information when marshaling...

View Article

Switching Database Profile using Spring Profiles

We are most likely to have separate db configuration for different environment like development and production environment. Spring profiles provide a convenient way to switch db profiles at runtime.

View Article

SQL and its Sub-Languages

SQL (Structured Query Language) is a language understood by most modern databases. It is an ANSI (American National Standard Institute) standard language which is used to manipulate databases.

View Article

Introducing JUnit Rule

Junit Rules allows developers to add additional functionalities that can applied to all test methods in a test class. It is similar to the concept of custom test runners but with reduced restrictions.

View Article

Addressing Ambiguity in Spring Autowiring

Spring autowiring is powerful concept, but we should be very cautious while using it. We may end up in creating ambiguity while autowiring beans, which will cause autowiring to fail.

View Article

Creating and Using Synonym in Oracle Database

Synonyms are database objects used to provide duplicate names to existing objects in the database. It is just an alternate name used to hide the original name of the object.

View Article

Creating and Using Sequence in Oracle Database

A sequence is used to auto-generate numbers in ascending or descending order which can serve as a primary key or a part of it (in case of composite key).

View Article

Creating and Manipulating Constraints in Oracle Database

Constraints are used to impose certain rules on columns to avoid invalid data entry into the table. If any of the constraint is violated the operation fails.

View Article

Integrating Log4J with Perf4J for Performance Logging

Perf4j is an open source logging framework used primarily for monitoring performance statistics in java applications. Log4j has the ability to integrate with perf4j to capture performance data.

View Article

Tagging in GIT

Tagging allows us to mark a specific point in the commit history or snapshot. A tag is typically used to mark a project release. This article shows how to create tags in Git.

View Article