5 min read

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:

  1. Configure the DispatcherServlet
  2. Configure the Controller
  3. Configure the View
  4. 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:

  1. Servlet mapping
  2. 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

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 and tags that are the child nodes of the tag. The following statement maps the DispatcherServlet to the name “dispatcher”.


   dispatcher
 
    org.springframework.web.servlet.DispatcherServlet
 

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 with value of 1 is also required. The modified tag will be:


    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.

URL mapping

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 and nodes of the node. The is used to refer the name that was mapped with the DispatcherServlet class. The is used to map a URL pattern with a servlet name so that when a request arrives matching the URL pattern, Application Server can redirect it to the mapped servlet. To map the DispatcherServlet with a URL pattern the tag will be:

dispatcher*.html

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 .xml. So if the DispacherServlet is mapped to the name dispatcher, then the name of the configuration file will be dispatcher-servlet.xml. The file will reside in WEB-INF folder of the application.

Everything in Spring Framework is a bean. Controllers are no exceptions. Controllers are configured as beans using the child tag of tag. A Controller is mapped by providing the URL of the request as the name attribute and complete qualified name of the Controller class as the value of the class attribute. For example, if the request URL is say, http://localhost/test/hello.html, then the name attribute will have /hello.html and the value attribute will have the fully qualified class name say, org.me.HelloWorldController. The following statements depicts the same:

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.


Subscribe to the weekly Packt Hub newsletter. We'll send you the results of our AI Now Survey, featuring data and insights from across the tech landscape.

* indicates required

LEAVE A REPLY

Please enter your comment!
Please enter your name here