Struts Tutorial
What is Struts?
Struts is a open source framework which make building of the web applications easier based on the java Servlet and JavaServer pages technologies.
Struts framework was created by Craig R. McClanahan and donated to the Apache Software Foundation in 2000. The Project now has several committers, and many developers are contributing to overall to the framework. Developing web application using struts frame work is fairly complex, but it eases things after it is setup. It encourages software development following the MVC design pattern. Many web applications are JSP-only or Servlets-only. With JSP and Servlets, Java code is embedded in the HTML code and the Java code calls println methods to generate the HTML code respectively. Both approaches have their advantages and drawbacks; Struts gathers their strengths to get the best of their association.
Struts is based on Model-View-Controller (MVC) design paradigm, it is an implementation of JSP Model 2 Architecture. For more of Model-View-Controller (MVC) click here . Consists of 8 Top-Level Packagesand approx 250 Classes and Interfaces.
Struts is a set of cooperating classes, servlets, and JSP tags that make up a reusable MVC 2 design. This definition implies that Struts is a framework, rather
than a library, but Struts also contains an extensive tag library and utility classes that work independently of the framework.
How Spring MVC Has Replaced the Struts Framework
Struts, once a popular Java-based framework for building web applications, has been largely replaced by **Spring MVC** due to its modern architecture, flexibility, and extensive ecosystem. Below is a detailed explanation of how and why Spring MVC has taken over Struts as the preferred choice for Java web development.
Key Reasons for Spring MVC Replacing Struts
-
Modern and Flexible Architecture
- Struts follows a Request-Response model with a fixed workflow, which can be restrictive for developers. Customization often requires significant effort.
- Spring MVC is based on a DispatcherServlet architecture, providing greater flexibility by allowing developers to configure handlers, interceptors, and views through a well-defined strategy interface.
-
Dependency Injection (DI) and Inversion of Control (IoC)
- Spring MVC is part of the larger Spring Framework, which offers DI and IoC out of the box. This makes it easy to manage and inject dependencies in a clean and decoupled manner.
- Struts lacks built-in support for DI and depends on external tools like Spring for similar functionality.
-
Ease of Integration
- Spring MVC integrates seamlessly with the broader Spring ecosystem, including Spring Security, Spring Data, and Spring Boot.
- Struts has limited integration capabilities and often requires additional configuration to work with other frameworks and libraries.
-
Annotations and Simplified Configuration
- Spring MVC leverages annotations like
@Controller
, @RequestMapping
, and @ModelAttribute
, reducing boilerplate XML configuration significantly.
- Struts 1 heavily relied on XML configuration, and while Struts 2 introduced annotations, its adoption came too late compared to Spring MVC.
-
View Technologies
- Struts was primarily tied to JavaServer Pages (JSP), which limited flexibility for modern view technologies.
- Spring MVC supports multiple view technologies, such as JSP, Thymeleaf, Freemarker, Velocity, and even RESTful JSON/XML responses, enabling developers to choose the best tool for their needs.
-
RESTful API Support
- Spring MVC natively supports building RESTful APIs, which are essential for modern web applications.
- Struts lacks robust built-in support for REST, requiring additional plugins or manual configurations.
-
Enhanced Exception Handling
- Spring MVC provides a clean way to handle exceptions through
@ExceptionHandler
and ControllerAdvice
.
- In Struts, exception handling is less flexible and often more complex to implement.
-
Community and Ecosystem
- The Spring ecosystem is vast, with active development and a strong community supporting it.
- Struts development has slowed over time, and it no longer has the same level of community or enterprise support.
-
Integration with Spring Boot
- Spring MVC works seamlessly with Spring Boot, which offers
Advantages of Spring MVC Over Struts
Feature |
Struts |
Spring MVC |
Configuration |
XML-heavy (Struts 1), Partial Annotation Support (Struts 2) |
Annotation-driven, minimal XML |
Architecture |
Rigid and limited flexibility |
Flexible and extensible |
REST Support |
Limited |
Full and native support |
Dependency Management |
Requires external frameworks |
Built-in with Spring Core |
Ecosystem |
Limited to web applications |
Part of the broader Spring ecosystem |
Testing |
Limited support |
Comprehensive testing capabilities |
Conclusion Spring MVC has replaced the Struts framework due to its flexibility, modern architecture, and robust ecosystem. Its seamless integration with other Spring projects, support for RESTful APIs, annotations-based configuration, and alignment with microservices trends make it the de facto choice for modern Java web development. Developers migrating from Struts to Spring MVC find a more productive and scalable environment for building web applications.
- Client Browser: An HTTP request from the client browser creates an event. The Web container will respond with an HTTP response.
- Controller: The controller is responsible for intercepting and translating user input into actions to be performed by the model. The controller is responsible for selecting the next view based on user input and the outcome of model operations. The Controller receives the request from the browser, and makes the decision where to send the request. With Struts,
the Controller is a command design pattern implemented as a servlet. The struts-config.xml file configures the Controller.
- Business logic: The business logic updates the state of the model and helps control the flow of the application. With Struts this is done with an Action class as a thin wrapper to the actual business logic.
- Model: model represents an application’s data and contains the logic for accessing and manipulating that data. Any data that is part of the persistent state of the application should reside in the model objects. The business objects update the application state. ActionForm bean represents the Model state at a session or request level, and not at a persistent level. Model services are accessed by the controller for either querying or effecting a change in the model state. The model notifies the view when a state change occurs in the model.The JSP file reads information from the ActionForm bean using JSP tags.
- View: The view is responsible for rendering the state of the model. The presentation semantics are encapsulated within the view, therefore model data can be adapted for several different kinds of clients. The view modifies itself when a change in the model is communicated to the view. A view forwards user input to the controller.The view is simply a JSP file. There is no flow logic, no business logic, and no model information, just tags. Tags are one of the things that make Struts unique compared to other frameworks like Velocity.