Understand the Struts 2 & Spring MVC
- Apache Struts 2 is an elegant, extensible framework for creating enterprise-ready Java web applications. The framework is designed to streamline the full development cycle, from building, to deploying, to maintaining applications over time. Apache Struts 2 was originally known as WebWork 2.
- Spring MVC, a Java Model-View-Contraller (MVC) web framework, which builds on the Spring Inversion of control(IoC) framework, extensive use of the Spring’s features make the Spring MVC framework highly decouple the components dependency and simplify the whole MVC configuration.
- Apache Struts 2 and SpringMVC, these two are the most popular and much talked about Java web frameworks today. Many of you might have worked with both of these frameworks, but which is one is better to use? What are the basic differences between both of these frameworks?
- Well, Apache Struts 2 is an elegant and extensible framework that is used for creating enterprise-level Java web applications. It is designed to streamline the development cycle, starting from building to deployment and maintenance of the application. In Struts, the object that is taking care of a request and routes it for further processing is known as “Action”.
- On the other hand, Spring MVC is a part of a huge Spring framework stack containing other Spring modules. This means that it doesn’t allow developers to run it without Spring, but the developers can run the Spring Core without Spring MVC. The Spring MVC (Model View Controller) is designed around a DispatcherServlet, which dispatches the requests to handler with configurable handler mappings, view resolution and theme resolution.
- While the objects responsible for handling requests and routing for processing in Struts called an Action, the same object is referred as Controller in Spring Web MVC framework. This is one of the very first differences between Spring MVC and Struts2. Struts 2 Actions are initiated every time when a request is made, whereas in Spring MVC the Controllers are created only once, stored in memory and shared among all the requests. So, Spring Web MVC framework is far efficient to handle the requests than Struts 2
- If we talk about the features, Struts 2 and Spring MVC framework caters different level of business requirements. Let’s take a look at features offered by both of these frameworks.
Features of Struts 2
- Configurable MVC components, which are stored in struts.xml file. If you want to change anything, you can easily do it in the xml file.
- POJO based actions. Struts 2 action class is Plain Old Java Object, which prevents developers to implement any interface or inherit any class.
- Support for Ajax, which is used to make asynchronous request. It only sends needed field data rather than providing unnecessary information, which at the end improves the performance.
- Support for integration with Hibernate, Spring, Tiles and so on.
- Whether you want to use JSP, freemarker, velocity or anything else, you can use different kinds of result types in Struts 2.
- You can also leverage from various tags like UI tags, Data tags, control tags and more.
- Brings ample support for theme and template. Struts 2 supports three different kinds of themes including xhtml, simple and css_xhtml.
- The Spring Web model-view-controller (MVC) framework is designed around a DispatcherServlet that handles all the HTTP requests and responses. The request processing workflow of the Spring Web MVC DispatcherServlet is illustrated in the following diagram:
- The Spring web MVC framework provides model-view-controller architecture and ready components that can be used to develop flexible and loosely coupled web applications. The MVC pattern results in separating the different aspects of the application (input logic, business logic, and UI logic), while providing a loose coupling between these elements.
- The Model encapsulates the application data and in general they will consist of POJO.
- The View is responsible for rendering the model data and in general it generates HTML output that the client's browser can interpret.
- The Controller is responsible for processing user requests and building appropriate model and passes it to the view for rendering.
Sequence of events
- Following is the sequence of events corresponding to an incoming HTTP request to DispatcherServlet:
- After receiving an HTTP request, DispatcherServlet consults the HandlerMapping to call the appropriate Controller.
- The Controller takes the request and calls the appropriate service methods based on used GET or POST method. The service method will set model data based on defined business logic and returns view name to the DispatcherServlet.
- The DispatcherServlet will take help from ViewResolver to pickup the defined view for the request.
- Once view is finalized, The DispatcherServlet passes the model data to the view which is finally rendered on the browser.
Features of Spring MVC
- Neat and clear separation of roles. Whether it is controller, command object, form object or anything else, it can be easily fulfilled with the help of a specialized object.
- Leverage from the adaptability, non-intrusiveness and flexibility with the help of controller method signature.
- Now use existing business objects as command or form object rather than duplicating them to extend the specific framework base class.
- Customizable binding and validation will enable manual parsing and conversion to business objects rather than using conventional string.
- Flexible mode transfer enables easy integration with the latest technology.
- Customizable locale and theme resolution, support for JSPs with or without Spring tag library for JSTL and so on.
- Leverage from the simple, but powerful JSP tag library known as Spring tag library. It provides support for various features like data binding and themes
What to Say ?
- Of course, Struts is one of the most powerful Java application frameworks that can be used in a variety of Java applications. It brings a gamut of services that includes enterprise level services to the POJO. On the other hand, Spring utilizes the dependency injection to achieve the simplification and enhance the testability.
- Both of these frameworks have their own set of pros and cons associated with it.
Benefits of Struts
- Simplified design
- Ease of using plug-in
- Simplified ActionForm & annotations
- Far better tag features
- OGNL integration
- AJAX Support
- Multiple view options and more
Drawbacks of Struts
- has compatibility issues
- poor documentation
Benefits of Spring MVC
- Clear separation between controllers, JavaBeans models and views that is not possible in Struts.
- Spring MVC is more flexible as compared to the Struts.
- Spring can be used with different platforms like Velocity, XLST or various other view technologies.
- There is nothing like ActionForm in Spring, but binds directly to the domain objects.
- Code is also more testable as compared to the Struts
- It is a complete J2EE framework comprising of seven independent layers, which simplifies integration with other frameworks.
- It doesn’t provide a framework for implementing the business domain and logic, which helps developers create a controller and a view for the application.
Drawbacks Spring MVC
- Complexity of the Spring framework.
- However, if you’re looking for the stable framework, Struts 2 is the right choice for you. On the other hand, if you’re looking for something robust, SpringMVC is perfect.
Produces of the HadoopExam Learning Resources
- Hadoop Professional Training with Hands On Practical Session for lifetime access
- CCD-410 Certification Material (Total 380 Questions + Revision Notes)
- CCA-410 Certification Material (Total 307 Questions + Revision Notes)
- CCSHB/CCB-400 : Total 214 Questions
- AWS Certified Solution Architect : 420+ Questions
- DS-200 (Data Science Certification)
- Recruitment Solutions :BigData/Hadoop/NoSQL/Cloud/
Click to View What Learners Say about us : Testimonials
We have training subscriber from TCS, IBM, INFOSYS, ACCENTURE, APPLE, HEWITT, Oracle , NetApp , Capgemini etc.