The first section will focus on configuring the application and its components so that the application can be deployed. The focus of the second section will be a real world application that will be developed using the steps described in the article on Developing the MVC components and in this article. That sets the agenda for this discussion.
Using Spring MVC – Configuring the Application
There are four main steps in configuring of the application. They are:
- Configure the DispatcherServlet
- Configure the Controller
- Configure the View
- Configure the Build Script
The first step will be same for any application that is built using Spring MVC. The other three steps change according to the components that have been developed for the application. Here are the details.
Configure the DispatcherServlet
The first step is to tell the Application server that all the requests for this ( Spring MVC based) application need to be routed to Spring MVC. This is done by setting up the DispatcherServlet. The reason for setting up DispatcherServlet is that it acts as the entry point to the Spring MVC and thus to the application. Since the DispatcherServlet interacts with the application as a whole (instead of individual components), its configuration or setting up at application level. And any setup that needs to be done at the application level is done by making the required entries in the web.xml.
The entries required in the web.xml can be divided into the following:
- Servlet mapping
- URL mapping
The former specifies the details of the servlet and the latter specifies how the servlet is related to a URL. Here are the details.
Servlet mapping is akin to declaring a variable. It is through servlet mapping that Application Server knows which servlets of the application it needs to support. Servlet mapping, in essence, assigns a name to a servlet class that can be reference throughout web.xml. To set up the DispatcherServlet, first it has to be mapped to a name. that can be done using
Since the DispatcherServlet needs to be loaded on the startup of the Application Server instead of the loading when a request arrives, the optional node
dispatcher org.springframework.web.servlet.DispatcherServlet 1
Next step is to map the URL to the servlet name so that the requests can be routed to the DispatcherServlet.
Once the servlet has been mapped, the next step is to map the servlet name with a URL so that the requests for that particular URL can be passed on to the application via the DispatcherServlet. That can be done using the
With this configuration/setting up of DispatcherServlet is complete. One point to keep in mind is that the URL pattern can be any pattern of one’s choice. However, it’s a common practice to use *.html for DispatcherServlet and *do for ActionServlet (Struts 1.x). Next step is to configure the View and Controller components of the application.
Mapping the Controller
By setting up the DispatcherServlet, the routing of requests to the application will be taken care of by the Application Server. However, unless the individual controllers of the application are setup/configured, the Framework would not know which controller to be called once the DispatcherServlet receives the request. The configuration of the Controller as well as the View components is done in the Spring MVC configuration file. The name of the configuration file is dependent on the name of the DispatcherServlet in web.xml, which is of the form
Everything in Spring Framework is a bean. Controllers are no exceptions. Controllers are configured as beans using the
One point to keep in mind is that the “/” in the bean name represents the relative path. In other words, /hello.html means that hello.html is directly under http://localhost/test. If hello.html was under another directory say, jsp which, in turn was directly under the application, then the name attribute will be /jsp/hello.html. Let us move onto configuring the Views.