Tips to Migrate from Traditional to Spring MVC based Java Web Application

0
This article talks about what would it take to move the non-spring Java application to Spring-based Java application. This essentially means that if you have a traditional Java web application with object instantiation taking place in independent class files or based on Factory pattern, this article would help you to move to a Spring based component model. Moving to Spring-based component model would essentially mean that one would move the object instantiation to one of the spring configuration file and that the component instantiation would happen based on dependency inversion principle. Before moving on to the steps, it would be good to learn Spring framework concepts and also Spring MVC concepts. It would be good to learn using samples of both annotation based and XML-based Spring MVC applications. In relation with MVC, Spring OOTB components such as Dispatcher Servlet, ViewResolver should be understood well.

 

[adsenseyu2]

 

Before we proceed to migration steps, it is key to understand What is Dispatcher Servlet?

 

The Dispatcher Servlet is mainly responsible taking an incoming URI and find the right combination of methods to be executed on Controller classes and, the views (generally JSPs) to be displayed along with the result from method execution. DispatcherServlet is nothing but Spring MVC’s implementation of the front controller pattern. Following code sample represents the fact that incoming URI is “/pages/contactus.html” upon which method executeContactUs method is called along with the view pages/contactus.jsp.
@RequestMapping(value="/pages/contactus.html")
private ModelMap executeContactUs() {
return somestuff;
}
Refer diagram below to get clear picture of how a web request is handled using Dispatcher Servlet:
DispatcherServlet Handling Incoming Web Request

DispatcherServlet Handling Incoming Web Request

 

Following are listed some of the key functionality of Dispatcher Servlet:
  • Mapping all the controllers provided which are annotated
  • Creating beans for all the classes which are annotated
  • Also configuring the dependencies between the classes and controllers.
Following are listed some of the key steps of migration:
  1. Migration from traditional JSP to JSPs consisting of Spring tags: In order to do this well, it would be good to learn concepts around spring mvc tags and their attributes. First and foremost, for all the spring tags to work, tag libraries need to be imported. Following are some key to-do things:
    1. The value to the path attribute of <form:input> tag should be same as POJO property.
    2. modelAttribute of spring form tag plays a vital role in passing objects from views to controllers and vice versa.
  2. Migration from traditional Controller to Spring-MVC based Controller: Following are listed some important details in this regard:
    1. The major changes be done while converting traditional controller to Spring controllers revolve basically around the annotations and mapping the requests to the corresponding methods in the controller.
    2. RequestMapping is used to map URL requests that come from the browser to the corresponding methods. The method attribute of RequestMapping annotation provides the information regarding the type of the request whether it is GET method or POST method.
    3. Every controller should be annotated with @Controller.
    4. In order to instantiate and inject objects, it is suggested to use @autowired annotation. This annotation helps to auto-wire the object.
  3. Configuration of Web.xml to initialize Spring Container (Application Context): This is the final step in which a mapping needs to be provided for the dispatcher servlet. The dispatcher servlet is represented using an XML file (responsible for handling beans and dependencies) and  whose mapping needs to be mentioned in web.xml. Following represents the configuration in web.xml file:
    Dispatcher Servlet Configuration in Web.xml

    Dispatcher Servlet Configuration in Web.xml

    Above configuration represents that all requests ending with .form will be handled by the example DispatcherServlet.

Once you are done with above, as a final step, create the war file and deploy it into tomcat and test the application. That is it!

Ajitesh Kumar

Ajitesh Kumar

Ajitesh has been recently working in the area of AI and machine learning. Currently, his research area includes Safe & Quality AI. In addition, he is also passionate about various different technologies including programming languages such as Java/JEE, Javascript and technologies such as Blockchain, mobile computing, cloud-native technologies, application security, cloud computing platforms, big data etc.

He has also authored the book, Building Web Apps with Spring 5 and Angular.
Ajitesh Kumar

Leave A Reply

Time limit is exhausted. Please reload the CAPTCHA.