Struts is an open-source Java framework that Apache designs. Apache provides users a free source to download Struts. Struts follow MVC architecture; Struts also consist of models: Model0, Model1, and Model2. It provides execution of MVC architecture. Struts provide pre-built classes for MVC that we can use or extend. Only developers can implement the Struct application.
Struts are used to develop web applications based on JSP and servlet. It provides predefined functionality due to which development and maintenance is simplified of web applications.
Versions of Struts
Struts have two versions:
🔗 1.x(1.0,1.1,1.2)
🔗 2.x(2.0,2.1,2.2)
Struts 1. x
✍️ Server-side Validation
✍️ i18N
✍️ Form backup Support
✍️ Exception Handling
✍️ Modularization
✍️ Multi button
✍️ Tiles Support
✍️ Plug-in support
Struts 2.x
This is the recent version by the Apache Software foundation. The features available in this version are as follows:
📌 It consists of filter based controller
📌 It uses both annotations and XML files for configuration.
📌 Supports zero configuration web application.
📌 implements AOP(Aspect Oriented Programming) from cross-cutting tasks with the help of interceptors.
📌 Uses the concept of DI.
📌 Inbuilt support for validation, file uploading, Ajax, etc.
📌Provides a custom tag library to create views etc.
Components of Struts
1. File Dispatcher:
File Dispatcher is a controller component of struts application. The controller is represented by org.Apache.Struts2.filtered patcher class, while it is just a filter class.
2. Action:
In Struts 2, action is used to represent the model's functionality. If we talk from a coding point of view, an action is represented by a bean class which contains the state of an application and any business logic. It is a model that is used to handle all the data.
3. Result:
The result simply means view. In struts2, the result component manages the functionality of the view. The result component is accountable for the presentation logic of the application.
4. Configuration file:
This configuration file describes results, actions, and other resources. The name of this file is "-struts.xml."
5. Interceptors:
These are the helper component of the controller and are accountable for applying cross-cutting concerns or other commonly used logic.
6. Deployment Descriptor:
It contains the information about controller web.xml. All the information collected in the deployment descriptor is used in MVC. It contains information about how many servlets and JSP are used in this application.
7. Tag Library:
Struts 2 has a custom tag library for developing JSP pages and customizing the data. The user can create the text or form with the help of a tag library. First, we need to make the tag directive in this library.
Structure of Struts.xml file
<struts>
<constant name = "struts.devMode" value = "true" />
<package name = "helloworld" extends = "struts-default">
<action name = "hello"
class = "hello.struts2.HelloWorldAction"
method = "execute">
<result name = "success">/HelloWorld.jsp</result>
</action>
<-- more actions can be filled here -->
</package>
<-- more packages can be entered here -->
</struts>
Commonly used tags of struts Library:
<S: form> : It is like a HTML tag. It is used to create Struts-based HTML forms.
<S: submit>: This element is used to create Submit button.
<S: text>: This tag is used to create a text field.
<S: textarea>: This element is used to create a text area.
Syntax:
<S: form name = “name & action”>
Sub element of form
</S: form>
Working of Struts
In the starting phase, the controller corrects a configuration file and uses it to deploy other control layer objects. These objects combined form struts in java configuration. The struts configuration defines the action mappings for an application.
Struts controller servlet contemplate the action mappings and route the HTTP requests to other components in the framework. First, the request is delivered to an action and then to JSP. The mapping helps the controller to modify HTTP requests into application actions. The action objects can handle the request form and respond to the client. Action objects have access to the servlet's methods and applications controller servlet. When delivering the control, an action object can indirectly forward more than one shared object, also JavaBeans, by creating them in the typical situation shared by java servlets.
Why Struts?
Increases reusability of code
Increases maintainability and extensibility
Affordable
No dependency on external sources
Robustness
Very realistic open-source project with a growing developer community
Advantages of Struts
Strut is an advanced framework with multiple features.
Struts follow MVC architecture
It has a simple configuration
Interceptor is present to reduce the cross-cutting functionality
Pluggable with multiple result types like JSP, Free maker, AJAX, velocity, etc.
OGNL
OGNL is nothing but an object graph navigation language. ONGL helps in expression language and type conversion. It is more effective as it can also initiate methods in java using expression language.
OGNL has form tags to create the form and to point to the bean and field in which the values have to be set. It has UI tags to display the results from various types like collections, arrays, etc. In addition, OGNL takes care of the type conversion to set the form values for the beans.
Using the action tag, programmers can act as the view page. By naming the activity, they can achieve this. To render the result in the view directly, they can set the "executeResult" option to "true."
What is the action name in Struts 2?
The most recent version of the Struts 2 framework is Struts 2.5. 22 was made available on November 29, 2019. The 2.5 Struts.
What is the return type of execute () method in struts2?
The framework has pre-defined return values for the action's execute() method, including SUCCESS, ERROR, INPUT, LOGIN, and NONE.
What is form bean in Struts 2?
Advertisements. By combining the set and push tags, the bean tag enables you to create a new instance of an object and then set the values of its variables. The bean is then made accessible in the value stack so the JSP page can use it.
Why is the Struts framework used?
Struts2 is an open-source framework that extends the Java Servlet API and supports the MVC structure. It helps us to create Java EE applications flexibly and efficiently. It supports the model, view, and controller by which the application is maintainable.
Conclusion
In this article, we learned about struts; we started by introducing struts, their version, features, components, advantages, OGNL, etc.