Categories: Java

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

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

 

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

    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

I have been recently working in the area of Data analytics including Data Science and Machine Learning / Deep Learning. I am also passionate about different technologies including programming languages such as Java/JEE, Javascript, Python, R, Julia, etc, and technologies such as Blockchain, mobile computing, cloud-native technologies, application security, cloud computing platforms, big data, etc. I would love to connect with you on Linkedin. Check out my latest book titled as First Principles Thinking: Building winning products using first principles thinking.

Recent Posts

Agentic Reasoning Design Patterns in AI: Examples

In recent years, artificial intelligence (AI) has evolved to include more sophisticated and capable agents,…

3 weeks ago

LLMs for Adaptive Learning & Personalized Education

Adaptive learning helps in tailoring learning experiences to fit the unique needs of each student.…

4 weeks ago

Sparse Mixture of Experts (MoE) Models: Examples

With the increasing demand for more powerful machine learning (ML) systems that can handle diverse…

1 month ago

Anxiety Disorder Detection & Machine Learning Techniques

Anxiety is a common mental health condition that affects millions of people around the world.…

1 month ago

Confounder Features & Machine Learning Models: Examples

In machine learning, confounder features or variables can significantly affect the accuracy and validity of…

1 month ago

Credit Card Fraud Detection & Machine Learning

Last updated: 26 Sept, 2024 Credit card fraud detection is a major concern for credit…

1 month ago