Struts is an open source framework developed by Apache Software foundation. It can be downloaded as a free source from apache website. Struts can be broadly classified as Struts 1.X and Struts 2.X. Though Struts 2.X is successor of Struts 1.X, there is a vast difference between them. Struts 2.x is based on WebWorks2 framework. Struts 2.x is also called as pull-MVC architecture because data that needs to be displayed to the user can be pulled from the Action(a class which contains business logic).
ActionServlet is the name of a class that plays role of controller in Struts. Whenever the user sends a request to server it passes via ActionServlet which then decides the necessary model and sends this request to the respective model.
Two more files support the flow of application:
Web.xml and struts-config.xml files.
Web.xml file is deployment descriptor which keeps all the application related settings while struts-config.xml file maps a request to Action classes and ActionForms(a simple POJO which contains properties related to UI).
Model is handled by various Java technologies like EJB, JDBC, Hibernate, Spring etc. It mainly concentrates on the business logic and semantics of the application. While view can be developed using JSP, Velocity Templates, JSTL, OGNL, XSLT and other HTML technologies.
View is responsible for getting the input from the user and rendering the result of that input sent back to user.
Let’s discuss Struts 2 architecture diagram. Struts 2 architecture is divided in 4 major categories.
1) Servlet filters
2) Struts core
4) User defined classes/files.
1) Servlet filters -Servlet filter comprises of 3 major components, those are ActionContextCleanUp, SiteMesh and FilterDispatcher.
When a user sends a request from browser to web server, the request reaches to ServletContainer. The request passes through the sequence of filters and cleanup classes. ActionContextCleanup is used when we integrate Struts with other Java technologies but it is an optional filter. SiteMesh is a framework helps maintaining consistent look & feel, navigations and layout across pages. It is also an optional filter. The next is FilterDispatcher. It is a required filter and used as a link between filters and ActionMapper(ActionMapper is the part of Struts Core). FilterDispatcher is also known as a bridge between Servlet Filters and Struts Core. FilterDispatcher handles Action execution, cleanup of context and can also provide static content of the application.
2) Struts Core : This is the actual Model part of Struts 2. It contains the main business logic. Struts Core category consists of Action, Interceptor,ActionProxy, ActionMapper, Result and Tag library systems.
Here, FilterDispatcher invokes ActionMapper to check whether incoming request is mapped to any action or interceptor or nothing. ActionMapper returns NULL if no Action invocation matches. ActionMapper can also return multiple Interceptors at a time. In this case, each interceptor is called one after the other. Here Interceptor is responsible for workflow implementation, avoids double submission problem, logging, validation, file upload etc.
Interceptor is the most useful feature of the Struts2.
3) Interceptors: In Struts 2, almost every action is associated with an interceptor or a set of interceptors. It can be called before or after the request processing. It can also be configured for each action basis individually. Interceptors define common cross cutting tasks in clean and reusable architecture.
VALUESTACK is a storage area which is used to store data associated with request processing. OGNL (Object Graph Navigation Language) provides the functionality of retrieving data stored in VALUESTACK. VALUESTACK is actually a stack which stores following objects in respective order.
a) Temporary Objects — These are the objects which are created during execution of action and placed onto the VALUESTACK. For e.g. various flags or local variables used in a JSP.
b) Model Objects — Current model object is placed on the VALUESTACK before the action class is executed.
c) Action Objects — Action objects are the actions which are being executed.
d) Named Objects — Application, Session, Request, Response etc are called as Named objects. These objects define the scope of variables.
4) User defined classes/files — Action classes, JSP or any other view and struts.xml etc are part of user defined classes or files. Action classes change the state of the model as per business requirement and return this state to the respective JSP or any other view with the help of struts.xml file. Application developer has to concentrate on user defined classes or files only. Rest all the components are taken care by the framework.
Comparison with Spring and Hibernate
- Struts is used to develop only frontend framework while Hibernate is used to develop backend systems. In contrast to above two, Spring is complete and modular framework i.e. it works on front end as well as backend development. So technically you can say Struts and Hibernate are subset of Spring framework.
- Struts uses Action class to write business logic while Spring uses Controller classes. Hibernate DAO classes contains logic related to the persistence of object.
- Action class in Struts is an abstract class while Controller class in Spring is Interface so performance vice Controller class of Spring is better than Action class of Struts. The main reason is a java class can only extend one class, but at the same time it can implement any number of interfaces, hence it is better to use interface rather than using abstract class.
- ActionForward in struts is replaced by ModelAndView object in Spring.
- Struts implements MVC architecture. Hibernate is ORM based framework. Spring framework is very modular and has multiple modules. Spring MVC module is based on MVC design pattern. It supports IOC, AOP, ORM etc.
- Main advantages of Spring framework is Transaction Management and Messaging support. In addition to this Spring very powerful with support of IOC, AOP, MVC, ORM and much more. Also Spring can be easily integrated with other frameworks like struts, hibernate etc.
- The advantages of Struts framework is elegant tag library support and easy integration with other front end technologies. The main advantage of Hibernate framework is database independency because Hibernate uses HQL to develop database independent queries.