Introduction to Java Struts Framework
In this article, I am going to give a brief introduction to Java Struts Framework. At the end of this article, you will understand the following pointers in detail.
- Basic MVC Architecture
- Why do developers care about MVC?
- What are Struts?
- Struts 2 Framework
- Struts 2 Features
- Struts 2 Advantages and Disadvantages
- Why struts so popular?
- Struts 2 based on MVC 2 architecture and its life cycle
- Struts 1.x vs Struts 2.x Differences
Basic MVC Architecture:
To handle different aspects of an application in development, the architecture components of the MVC pattern are designed. The MVC design pattern serves to separate the presentation layer from the business logic.
Why do developers care about MVC?
MVC is the most used software design pattern in the app and web development. The design pattern consists of –
For better understanding, please have a look at the following diagram.
Let us understand the role and responsibilities of Model, View, and Controller.
Model: stores & manages data
- It consists of systems’ business logic.
- It contains all the data of the application.
- It represents the state of the application.
- It consists of those classes that have a connection to the database.
- The controller makes a connection with the model, fetches the data, and sends it to the view layer.
- The model connects with the database and stores the data into a database that is connected to it.
View: Graphical User Interface
- It consists of HTML, JSP, etc. into it.
- It represents the UI of the application.
- It is used to show the data that is being fetched from the controller, in turn, it fetches data from model layer classes.
- This view layer shows the data on the UI of the application.
Controller: Brains of the application
- The controller connects the model and view.
- The controller converts inputs from view to fulfill the demand to retrieve/update data in the model.=
- The controller receives input from view and uses logic to convert the input to a demand for the model that grabs the data and the controller passes data to the view from the model back for the user for better display.
What Are Struts?
Struts are frameworks that are based on a set of Java technologies such as JSP, Servlet, JSTL, XML, and many more that integrate MVC architecture. This framework gives a ready for the use of framework validation. The power of Struts lies in the layer of the model through which Struts can be implemented with different Java technologies such as Spring, JDBC, Hibernate, etc.
Struts is also defined as an open-source framework that is developed by the Apache Software Foundation and can be easily downloaded from the apache website. It can be
classified as Struts 1.X and Struts 2.X. There is a vast difference between Struts 1.X and Struts 2.X, Struts 2.X is the successor of Struts 1.X. Struts 2.x, which is fully based on the WebWorks2 framework and it is also known as pull-MVC architecture and this is called because the data which has to be displayed to the user, it can be easily pulled from the Action(a class which contains business logic).
ActionServlet defines the class which plays the major role of controller in Struts. As soon as the user sends a request to the server, it passes to the ActionServlet, which then suggests the major model and sends the request to the respective model. More than two files support the application flow: web.xml and struts-config.xml files. Web.xml file is a deployment descriptor that handles every application related to settings and the struts-config.xml file maps a request to ActionForms(a simple POJO which contains properties related to UI) and Action classes. Models are supported through numerous Java technologies such as JDBC, Spring, Hibernate, and many more. It mainly focuses on the business semantics of the application. Velocity Templates, JSTL, JSP, OGNL, XSLT technology can be developed by the view. It is responsible for taking the input from the user and showing the output of that input and sent back to the user.
Struts 2 Framework
Struts2 Framework is a popular web application framework that is based on the MVC design pattern. It is not just a new version of Struts 1, but it is also a fully rewrite of the Struts architecture.
Initially, the Webwork framework started with the Struts framework and its motto was to render a well-developed and enhanced framework that is built on Struts in order to design web development easier for the developers. After a certain while, the Struts community and the Webwork framework both joined hands to create the famous Struts2 framework.
Struts 2 Framework Features
Below given is the great features which focus you to consider Struts2 −
- POJO Forms and POJO Actions − You can use any POJO to receive the form input with the help of Struts 2. you can also see any POJO as an Action class.
- Tag Support − Struts2 has impoverished form tags and the new form tags which allows us to write the code to the developer.
- Easy Integration − Integration with different frameworks such as Spring and SiteMesh is now becoming easier with a multiple of integration available with Struts2.
- Template Support − By using templates, we can support the generating of views.
- Plugin Support − The core Struts2 performances can be enlightened with the help of plugins Support. The number of plugins Support is available for Struts2.
- Profiling − Struts2 provides integral profiling to debug and profile the application. With the help of built-in debugging tools, Struts also provides an integrated debugging.
- Easy to Modify Tags − Tag markups in Struts2 can be used by Freemarker templates. JSP or Java knowledge is not required. Basic knowledge of XML, HTML, and CSS knowledge is enough to modify the tags.
- Promote Less configuration − By using default values for various settings, Struts2 provides a less configuration. You don’t map to configure anything until it deviates from the default settings set by Struts2.
- View Technologies − Struts2 has large support for various view options such as Freemarker, JSP, Velocity, and many more.
Above are the major features of Struts 2 that make it an Enterprise framework.
Struts 2 Advantages and Disadvantages
- Simplified Design: Code is not so closely coupled to the Struts framework.
- Easy plug-in: Other technologies can be easily used by developers to plug-in easily. It also includes Spring, SiteMesh, and many more.
- Simplified ActionForm: ActionForms such as POJOs, we don’t need to extend from any class or implement any interface.
- Annotations introduced: Uses of annotation results in length reduction and code complexity. It also includes configuration files for simplicity.
- Better tag features: It is used in Ajax-enabled tags and theme-based tags.
- Simplified Testability: Unit testing of Struts 2 Action class is easier because it doesn’t want complex HttpServletResponse and HttpServletRequest.
- Simplified Action: Simplified ActionForms are simple POJOs and they do not need to extend any class or implement any interface.
- OGNL integration: To fetch data from ValueStack and type conversion that reduces the code, it uses OGNL.
- Ajax support: Struts2 tags are enabled Ajax.
- Multiple View options: JSP is not restricted by View. Velocity templates, Freemarker can also be used as a view.
Although Struts 2 comes with large features, there are some boundation of the latest version – Struts 2 needs to be further enhanced. Points that are given below are the major features −
- Bigger Learning Curve – With the help of MVC with Struts, you must comfort with the standard JSP, Servlet APIs, and a wide & elaborate framework.
- Poor Documentation – Compared to the JSP APIs and standard servlet, Struts has a few numbers of online resources, and many first-time users show the online Apache documentation confusing and poorly organized.
- Less Transparent – There are numerous things that are going behind the scenes rather than normal Java-based Web applications which is a very difficult task to understand the framework.
So a fine framework must provide generic behavior with various types of applications that can make use of. The best web framework is Struts 2 and it is highly used for the development of Rich Internet Applications (RIA).
Struts 2 Based on MVC 2 Architecture
From a top-level view, basically Struts 2 is an MVC2 framework. The five core components of the Model-View-Controller pattern in Struts 2 are –
- Value Stack / OGNL
- Results / Result types
- View technologies
Struts 2 is slightly different from a traditional MVC framework, where the action takes the role of the model rather than the controller, although there is some overlap.
From the above diagram, it can be seen that –
- The controller is implemented using a Struts 2 dispatcher servlet filter and interceptors.
- The model is implemented using actions.
- The view is implemented using result types and results.
- The value stack and OGNL can be used to provide common thread, linking, and enabling integration among the other components.
Now let us discuss the flow of the Struts 2 Architecture –
- Container checks the request in the web.xml file and gets the class name of the controller.
- Container forms the controller (StrutsPrepareAndExecuteFilter or FilterDispatcher). Since struts2.1, it is StrutsPrepareAndExecuteFilter. Before 2.1 it was FilterDispatcher.
- The information gets by the controller for the action from the ActionMapper
- Controller form the ActionProxy
- ActionProxy gets the information of the action and interceptor stack from the configuration manager that gets information from the struts.xml file.
- ActionProxy is than forwards the request to the ActionInvocation
- ActionInvocation invokes each interceptor and action
- A result is generated
- The result is then sent back to the ActionInvocation
- A HttpServletResponse is than generated
- The response is then sent to the user.
Struts 1.x vs Struts 2.x Differences
|Feature||Struts 1||Struts 2|
|Action classes||Struts1, through the action class it extends the abstract base class. The main issue with struts1 is that it uses abstract classes rather than interfaces.||In Struts 2, an Action interface is implemented by Action class, along with other interfaces that use custom and optional services. It also supports a base ActionSupport class that implements commonly used interfaces. We can use POJO objects with an execute signature as a Struts 2 action object because an Action interface is not mandatory.|
|Threading Model||Struts 1 Actions must be thread-safe because all the requests for that Action are handled by only one instance of a class so Struts 1 Actions are singletons. The singleton strategy binds to Struts 1 Actions and provides more care to make the action resources synchronized or thread-safe while developing an application.||Struts 2 doesn’t have thread-safety issues because each request is instantiated by an Action object. A servlet container provides many throw-away objects per request|
|Servlet Dependency||When an Action is invoked, Actions are dependent on the servlet API, HttpServletRequest and HttpServletResponse is passed to the execute method.||As a couple, Container does not treat the Struts 2 Actions. In isolation, Action is to be tested and allows a Servlet context as simple Maps. Original request and response can still be accessed by Struts 2 Action if required. While other architectural elements directly eliminate the need to access the HttpServetRequest or HttpServletResponse.|
|Testability||While testing an application, Struts1 application has a major problem because Servlet API is exposed by the execute method. Struts TestCase gives a set of mock objects for Struts 1.||While testing an application, Struts1 application has a major problem because Servlet API is exposed by the execute method. Struts TestCase gives a set of mock objects for Struts 1.|
|Harvesting Input||By creating an ActionForm object, Struts 1 receives an input. Like the action classes, the ActionForm base class must be extended by all ActionForm classes. Other JavaBeans classes cannot be used as ActionForms, while to receive the input, developers create redundant classes. The best alternative to create conventional ActionForm classes is DynaBeans.||Action properties are required by Struts 2 as input properties that delete the need of a second input object. And these Input properties may be rich object types, since they may have their own properties. Action properties can be accessed by Developers from the web page using the taglibs. Struts 2 also provides the ActionForm pattern, POJO form objects, and POJO Actions.|
|Expression Language||Struts1 is integrated with JSTL, hence it uses the JSTL EL. The EL has a relatively weak collection but has basic object graph traversal.||Struts 2 uses JSTL, but the framework suggests a more flexible expression language known as “Object Graph Notation Language” (OGNL).|
|Binding values into views||By using the standard JSP mechanism, Struts 1 binds objects into the page context.||ValueStack technology is used by Struts 2 to make the values applicable to the taglibs without coupling the view to the object to which it is rendering. To reuse views across a range of types, the ValueStack strategy is enabled which is having the same property name but different property types.|
|Type Conversion||ActionForm Struts 1 properties are in the form of Strings. Struts 1 uses the Commons-Beanutils for type conversion. Converters are per-class which are not configured per instance.||OGNL is used by Struts 2 for type conversion and converters to convert Basic and common object types and primitives also.|
|Validation||Manual validation is used by Struts 1 which is done via the validate method on the ActionForm.||By using the validate method and the XWork Validation framework, Struts 2 also makes manual validation. The Xwork Validation Framework allows the chaining of validations into sub-properties using the validations defined for the properties class type and the validation context.|
|Control of Action Execution||Each module in Struts 1 has separate Request Processors (lifecycles), while all the Actions in the module must share the same lifecycle.||In Struts, 2 different lifecycles are created on a per Action basis via Interceptor Stacks. Custom stacks are created and used with different Actions, as required.|
In the next article, I am going to discuss Model 1 and Model 2 Architecture in detail. Here, in this article, I try to give a brief introduction to Java Struts Framework and I hope you enjoy this Introduction to Java Struts Framework article.