Introduction
In this tutorial, you will learn all about all the basic concepts, elements and technologies related to Struts. This comprehensive foundational knowledge will give you a fundamental understanding of Struts, that will help you in easily understanding Struts when learning it at an advanced level. Our tutorial is specifically curated for the beginners and freshers, so that they can use this as a guide in their Struts learning. So, explore further down below in this tutorial to have a rich and useful foundation in Struts.
Struts Tutorial – Fundamental Concepts in Struts
The following are some of the fundamental concepts in Struts:
- Model-View-Controller (MVC) Architecture: Struts adheres to the MVC design pattern, dividing the application into three components:
- Model: It encompasses the application’s data and business logic.
- View: This layer, typically composed of JSP (JavaServer Pages) pages, presents information to users.
- Controller: Responsible for managing the application’s flow, the controller handles user requests, invokes relevant business logic within the model, and directs responses to the view.
- Action Servlet: Serving as the controller component, the ActionServlet intercepts incoming HTTP requests. It then assigns processing tasks to appropriate Action classes based on factors like the request URL.
- Action: Java classes known as Actions handle client requests. Each action corresponds to a specific user interaction, such as form submission or link clicking. Actions process requests, interact with the model to execute business logic, and forward results to the appropriate view.
- Action Mapping: Action mappings establish connections between incoming requests and corresponding action classes. Typically configured in the struts-config.xml file (or via annotations in newer Struts versions), they specify which action class should manage each type of request.
- Form Beans: These are Java objects representing HTML forms, encapsulating user-submitted data. Form beans facilitate form input validation and processing, separating presentation from the logic necessary for handling data.
- Validation Framework: Struts offers an integrated validation framework allowing developers to declaratively define validation rules for form beans. These rules, specified in XML configuration files or through annotations, are automatically enforced when processing form submissions.
- Struts Tag Library: Struts provides a tag library (Struts Taglib) offering custom JSP tags for building user interfaces. These tags streamline the creation of dynamic web pages by encapsulating common tasks like form rendering, input validation, and error handling.
Fundamental Elements in Struts
The following are some of the fundamental elements in Struts:
- ActionServlet: At the heart of Struts lies the ActionServlet, serving as the pivotal controller component. It’s responsible for receiving incoming HTTP requests and directing them to the appropriate Action classes based on predefined mappings.
- Action: Within Struts, Actions are Java classes assigned with the task of processing specific user requests or interactions within the application. They encapsulate the corresponding business logic and often interact with the model layer to fetch or update data.
- Action Mapping: Defining the linkage between incoming requests and their corresponding Action classes, action mappings are crucial components. These mappings can be configured either through the traditional struts-config.xml file or via annotations in newer Struts versions.
- Form Bean: Form beans, integral to Struts applications, represent HTML forms within the system. They encapsulate user-submitted data, often incorporating validation logic to maintain data integrity.
- Validation Framework: Struts boasts a comprehensive validation framework, empowering developers to define validation rules for form beans. These rules can be expressed declaratively using XML configuration files or annotations. They’re automatically enforced during the processing of form submissions.
- Struts Tag Library (Struts Taglib): The Struts Taglib stands as a cornerstone in simplifying the creation of dynamic user interfaces. This library furnishes a collection of custom JSP tags, streamlining common tasks like form rendering, input validation, and error handling. It enables developers to construct web pages with greater efficiency.
Basic technologies related to Struts
The following are the technologies related to Struts:
- Java Servlets: Struts relies on Java Servlets at its core. Servlets, Java classes responsible for processing requests and generating responses dynamically, are fundamental to web applications. The ActionServlet in Struts, a subclass of HttpServlet, manages incoming requests and directs them to appropriate actions.
- JavaServer Pages (JSP): JSP, a technology for creating dynamic web pages in Java, plays a significant role in Struts. Typically used for the presentation layer (the View in MVC), JSP pages are integral to Struts applications. The Struts Tag Library (Struts Taglib) offers custom JSP tags to simplify tasks like form rendering and validation.
- JavaBeans: Struts leverages JavaBeans, reusable Java components, to manage form data. Form Beans, a type of JavaBean in Struts, represent HTML forms and facilitate the transfer of data between the presentation layer (JSP pages) and the business logic layer (Action classes).
- XML Configuration: Struts usually employs XML configuration files to define various components and mappings within the framework. For instance, the struts-config.xml file configures action mappings, form beans, and global forwards, providing a structured approach to configuration.
- Apache Tomcat: Apache Tomcat, an open-source web server and servlet container, is commonly used to deploy Java web applications, including those built with Struts. It serves as the runtime environment for executing servlets and JSP pages, facilitating the deployment and execution of Struts applications.
- Apache Maven: Maven, a powerful build automation tool for Java projects, finds extensive use in Struts development. With capabilities to manage dependencies, compile code, run tests, and package applications, Maven simplifies project management tasks. Many Struts projects adopt Maven for dependency management and building applications efficiently.
- Hibernate or JDBC: Though not directly tied to Struts, databases are integral to web applications for data storage and retrieval. Hibernate, an ORM framework, abstracts database interactions, simplifying data persistence in Java applications. JDBC, on the other hand, is a lower-level API facilitating direct connectivity between Java applications and databases. These technologies play a crucial role in integrating Struts applications with database systems.
Example demonstrating a simple login functionality using Struts.
- Login Action:
package com.example.struts.action;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport {
private String username;
private String password;
public String execute() {
return (“admin”.equals(username) && “password”.equals(password)) ? SUCCESS : ERROR;
}
// Getters and setters for username and password
}
- Login Form Bean:
package com.example.struts.form;
public class LoginForm {
private String username;
private String password;
// Getters and setters for username and password
}
- Struts Configuration (struts.xml):
<?xml version=”1.0″ encoding=”UTF-8″?>
<!DOCTYPE struts PUBLIC “-//Apache Software Foundation//DTD Struts Configuration 2.0//EN” “http://struts.apache.org/dtds/struts-2.0.dtd”>
<struts>
<package name=”default” extends=”struts-default”>
<action name=”login” class=”com.example.struts.action.LoginAction”>
<result name=”success”>/success.jsp</result>
<result name=”error”>/error.jsp</result>
</action>
</package>
</struts>
- JSP Pages:
login.jsp:
<%@ page contentType=”text/html;charset=UTF-8″ language=”java” %>
<%@ taglib prefix=”s” uri=”/struts-tags” %>
<html>
<head>
<title>Login</title>
</head>
<body>
<h2>Login</h2>
<s:form action=”login” method=”post”>
<s:textfield label=”Username” name=”username”/>
<s:password label=”Password” name=”password”/>
<s:submit value=”Login”/>
</s:form>
</body>
</html>
- success.jsp:
<%@ page contentType=”text/html;charset=UTF-8″ language=”java” %>
<html>
<head>
<title>Login Successful</title>
</head>
<body>
<h2>Login Successful</h2>
</body>
</html>
- error.jsp:
<%@ page contentType=”text/html;charset=UTF-8″ language=”java” %>
<html>
<head>
<title>Login Error</title>
</head>
<body>
<h2>Login Error. Please try again.</h2>
</body>
</html>
- Web Deployment Descriptor (web.xml):
Configure servlet mapping and listener for Struts.
- Compilation and Deployment:
Compile Java classes and JSP pages, package the application, and deploy it to a servlet container like Apache Tomcat.
Conclusion
Understanding these core elements is pivotal for proficiently crafting web applications using the Struts framework. They establish the foundation for organizing the application’s architecture, managing user interactions, and implementing business logic. We hope that this tutorial gives you the basic knowledge on Struts that will give you enough knowledge to pursue this concept into an advanced level.