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:
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:
- 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:
- The value to the path attribute of <form:input> tag should be same as POJO property.
- modelAttribute of spring form tag plays a vital role in passing objects from views to controllers and vice versa.
- Migration from traditional Controller to Spring-MVC based Controller: Following are listed some important details in this regard:
- 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.
- 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.
- Every controller should be annotated with @Controller.
- In order to instantiate and inject objects, it is suggested to use @autowired annotation. This annotation helps to auto-wire the object.
- 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:
Above configuration represents that all requests ending with
.form
will be handled by theexample
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!
Latest posts by Ajitesh Kumar (see all)
- Agentic Reasoning Design Patterns in AI: Examples - October 18, 2024
- LLMs for Adaptive Learning & Personalized Education - October 8, 2024
- Sparse Mixture of Experts (MoE) Models: Examples - October 6, 2024
I found it very helpful. However the differences are not too understandable for me