Search This Blog

Loading...

Sunday, 4 November 2012

Basic MVC application in Spring

Spring provides support for MVC based web application development. I decided to create a very basic application involving a simple HTML page and a JSP page. I created a simple dynamic web project using eclipse:

This implementation uses zero annotations and simply attempts to develop a very basic MVC application focusing on the actual flow of control within Spring.
The first step is to define the DispatcherServlet. Spring's web support is built around the front controller pattern. This requires the definition of a single servlet which receives all requests and then delegates the responsibility of processing theses requests to appropriate controllers. In Spring we have the front controller servlet predefined as org.springframework.web.servlet.DispatcherServlet.
We simply need to configure it in our web.xml

We have defined that all requests ending with .do are redirected to the servlet.
When the servlet container loads the DispatcherServlet it

executes the init() method as a part of Servlet lifecycle. The DispatcherServlet inherits its implementation from org.springframework.web.servlet.HttpServletBean. The call of execution is made to the abstract method initServletBean()

the abstract method is implemented by org.springframework.web.servlet.FrameworkServlet. The method is responsible for loading the WebApplicationContext for this application. (via the initWebApplicationContext() method.

the WebApplicationContext is loaded via an XML file with name as <dispatcher servlet name>-servlet.xml. In this case springDispatcher-servlet.xml.

The Controller implements the handleRequest method that receives a request and response object. The controller can process the request and generate a suitable response. It then forwards the response to a suitable view which will be displayed.(Implementing the controller is not needed anymore with the arrival of annotations.) But how does the controller decide which view ?
This is where the ModelAndView return value comes into picture. The class is a holder for both Model and View in the web MVC framework. It returns to the controller both model and view in a single return value.
In the above code we do not have any model data. We simply return the view to be used.
The Servlet now needs to decode the view. For that I used a ViewResolver. The job of the view-resolver is to convert a view name into an actual view.
As our view is a jsp file , I used a simple InternalResourceViewResolver class that converts welcome into welcome.jsp.

This still leaves one question open. How does the Servlet select the controller to use ?
For this we defined a UrlMapping object. We specified in our xml file that requests ending in "anything.do" must be sent to the bean with name "defaultController".
We shall look into the details of this object in later posts.
The last thing was the welcome page which simply redirects to the controller

About Me

I am currently pursuing my masters in Computer Science at the University Of Texas at Dallas. Back in India I worked as a software engineer in the area of Java/J2EE for over five years. My experience is primarily in the financial and enterprise domain. I like tinkering around with the tools of my work and sharing the knowledge gained.