Struts
Struts
Struts
• Struts is used to create a web applications based on servlet and JSP.
• Struts depend on the MVC (Model View Controller) framework. Struts application is a genuine web
application.
• Struts are thoroughly useful in building J2EE (Java 2 Platform, Enterprise Edition) applications because struts
takes advantage of J2EE design patterns. Struts follows these J2EE design patterns including MVC.
• In struts, the composite view manages the layout of its sub-views and can implement a template, making
persistent look and feel easier to achieve and customize across the entire application. A composite view is made
up by using other reusable sub views such that a small change happens in a sub-view is automatically updated
in every composite view.
• Struts consists of a set of own custom tag libraries.
• Struts are based on MVC framework which is pattern oriented and includes JSP custom tag libraries. Struts also
supports utility classes.
• Struts is an open source framework that extends the Java
Servlet API and employs a Model, View, Controller (MVC)
architecture.
• It enables you to create maintainable, extensible, and
flexible web applications based on standard technologies,
such as JSP pages, JavaBeans, resource bundles, and
XML.
Components of Struts in Java
• In Struts 2, the functionality of the model is represented by the action component. From
a coding point of view, an action is represented by a bean class containing the state of
an application and any business logic. This component is developed by the programmer.
An Action is a model in Struts 2 which is used to handle all the data.
3. Result
• The result means view. In the Struts2 application, the functionality of view is managed
by the result component. That is, the result component is responsible for the
presentation logic of the Struts application.
• A result is responsible for identifying JSP pages to display the results.
4. Configuration file
• Struts 2 uses a configuration file to describe the action, result and other
resources. The name of this file is — struts.xml.
5. Interceptors
• Interceptors are the helper components of the controller and are
responsible for applying cross-cutting concerns or other commonly used
logics. Wherever we want to use the logic in JSP or servlet, we use
interceptors. There are many interceptors in Struts.
6. Deployment Descriptor
• This is the deployment descriptor of the Struts Application and contains the
information about controller web.xml. All information gathered in the
deployment descriptor, which is used in MVC. It stores information about how
many JSP and servlets used in this application. Basically, it is the XML file.
7. Tag Library
• Struts 2 provides a custom tag library to develop JSP pages and to manipulate the
data. The user can design the form or text using the tag library. First, we make
the tag directive in this library.
Features of Struts:
• Struts encourages good design practices and modelling because the framework is designed with “time-proven” design
patterns.
• Struts is almost simple, so easy to learn and use.
• It supports many convenient features such as input validation and internationalization.
• It takes much of the complexity out as instead of building your own MVC framework, you can use struts.
• Struts is very well integrated with J2EE.
• Struts has large user community.
• It is flexible and extensible, it is easy for the existing web applications to adapt the struts framework.
• Struts provide good tag libraries.
• It allows capturing input form data into javabean objects called Action forms.
• It also hand over standard error handling both programmatically and declaratively.
Working of Struts:
• In the initialization phase, the controller rectify a configuration file and used it to deploy other control layer objects.
• Struts configuration is form by these objects combined together. The struts configuration defines among other things
the action mappings for an application.
• Struts controller servlet considers the action mappings and routes the HTTP requests to other components in the
framework.
• Request is first delivered to an action and then to JSP.
• The mapping helps the controller to change HTTP requests into application actions.
• The action objects can handle the request from and responds to the client (generally a web browser). Action objects
have access to the applications controller servlet and also access to the servlet’s methods.
• When delivering the control, an action objects can indirectly forward one or more share objects, including
javabeans by establish them in the typical situation shared by java servlets.
MVC Architecture
• MVC means Model-View-Controller where model is responsible for business logic, view handles
the user interaction with application and controller decides the flow of application.
• Figure 1.1 explains the working methodology behind MVC architecture which is as follows:
1. User sends the request using view which reaches to the controller on the server.
2. Controller decides the respective model and delivers the control to particular model.
3. As a result of business processing, model changes its state.
4. Depending on the state change of model, control redirects the flow to the view. Main
advantage of this architecture is the separation of data layer, presentation layer and pure
business logic.
MVC Architecture
MVC Architecture
• Model: Model is responsible for providing the data from the database and saving the data into the data
store. All the business logic are implemented in the Model. Data entered by the user through View are
check in the model before saving into the database. Data access, Data validation and the data saving
logic are part of Model.
• View: View represents the user view of the application and is responsible for taking the input from the
user, dispatching the request to the controller and then receiving response from the controller and
displaying the result to the user. HTML, JSPs, Custom Tag Libraries and Resources files are the part of
view component.
• Controller: Controller is intermediary between Model and View. Controller is responsible for receiving
the request from client. Once request is received from client it executes the appropriate business logic
from the Model and then produce the output to the user using the View component. ActionServlet,
Action, ActionForm and struts-config.xml are the part of Controller.
Advantages of Struts
•JDBC Setup:
•Create a DataSource in your web.xml or use a connection pool (like Apache Commons DBCP or C3P0).
•Write a DAO (Data Access Object) class to interact with the database
Example DBConnection.java (using JDBC):
• import java.sql.*;
public class DBConnection {
• public static Connection getConnection() {
• try {
• Class.forName("com.mysql.cj.jdbc.Driver");
• return DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb", "username", "password");
• } catch (Exception e) {
• e.printStackTrace();
• return null;
• }
• }
• }
• Action Class with Database Connectivity: In Struts,
actions are used to process user requests. You can
interact with the database from the action class.
• Example LoginAction.java:
• import org.apache.struts.action.Action;
• import org.apache.struts.action.ActionForm;
• import org.apache.struts.action.ActionMapping;
• import org.apache.struts.action.ActionForward;
<dependencies>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts</artifactId>
<version>1.3.10</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.26</version>
</dependency>
</dependencies>
•Deploy the Application:- Once you've built the WAR file, deploy it to your servlet container (e.g., Tomcat).
•Copy the WAR file to the webapps directory of your Tomcat server.
•Start or restart the Tomcat server.