Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
32 views10 pages

Introduction To Apache Struts 2 A Basic Tutorial

This document provides an introductory tutorial on Apache Struts 2, an open-source MVC framework for building Java web applications. It covers core concepts, key components, project setup, and configuration, culminating in a simple 'Hello World' application example. The tutorial encourages further exploration of advanced topics such as interceptors, validation, and AJAX integration.

Uploaded by

sddeepanbabu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views10 pages

Introduction To Apache Struts 2 A Basic Tutorial

This document provides an introductory tutorial on Apache Struts 2, an open-source MVC framework for building Java web applications. It covers core concepts, key components, project setup, and configuration, culminating in a simple 'Hello World' application example. The tutorial encourages further exploration of advanced topics such as interceptors, validation, and AJAX integration.

Uploaded by

sddeepanbabu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Introduction to Apache

Struts 2: A Basic
Tutorial
This presentation offers a concise introduction to Apache Struts 2,
an open-source MVC web application framework widely adopted for
building robust Java web applications. We'll cover its core concepts
and walk through a "Hello World" example.
Understanding MVC Architecture
Model
Encapsulates application data and business logic, independent of the user interface.

View
Responsible for presenting data to the user, typically using technologies like JSP or Freemarker.

Controller
Processes user input, orchestrates interactions between the Model and View. In Struts 2, the Action serves
this role.

The Model-View-Controller (MVC) pattern separates concerns, enhancing modularity and maintainability in web
applications.
Key Struts 2 Components
Actions Results
Plain Old Java Objects (POJOs) that encapsulate the Define what view (e.g., a JSP page or redirect) should
business logic and respond to user requests. be rendered after an Action's execution.

Interceptors Value Stack & OGNL


Objects that can execute code before or after an The Value Stack is where data is stored for access by
Action invocation, providing cross-cutting concerns the View, often manipulated using the Object-Graph
like validation or logging. Navigation Language (OGNL).
Project Setup: Dependencies
Prerequisites
<dependency> <groupId>org.apache.struts</groupId>
• JDK 8 or higher <artifactId>struts2-core</artifactId>
• Maven 3.6 or higher <version>2.5.33</version></dependency>

• An IDE (e.g., IntelliJ IDEA, Eclipse)

Steps

1. Create a new Maven Webapp project.


Edit your pom.xml file.
Add the struts2-core dependency.
`web.xml` Configuration
Purpose
<filter> <filter-name>struts2</filter-name> <filter-class>
The web.xml file, located in src/main/webapp/WEB-INF/, is the deployment descriptor for your web org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter </filter-
application. It registers the Struts 2 filter. class></filter><filter-mapping> <filter-name>struts2</filter-name> <url-
pattern>/*</url-pattern></filter-mapping>
Key Configuration

You need to register the StrutsPrepareAndExecuteFilter to intercept all incoming requests,


allowing Struts 2 to process them.
`struts.xml` Configuration
Central Configuration

The struts.xml file, found in src/main/resources, is the heart


of Struts 2 configuration. It defines how actions are mapped to
Java classes and what results correspond to specific views.

Example Mapping

Here, we define an action named "hello" that executes the


HelloWorldAction class. Upon successful execution, it redirects
to the hello.jsp view.

<package name="default" namespace="/"


extends="struts-default"> <action name="hello"
class="com.example.HelloWorldAction">
<result name="success">/hello.jsp</result>
This XML file is critical for defining the navigation flow and
</action></package>
component interactions within your Struts 2 application.
The `HelloWorldAction.java` Class
The Action class is a simple Java POJO that contains the business logic for your web request.

Key Characteristics
package com.example;import
It should contain an execute() method, which returns a string result (e.g., com.opensymphony.xwork2.ActionSupport;public class
Action.SUCCESS). HelloWorldAction extends ActionSupport { private String
• Any data you want to display on the JSP page should be exposed via message; public String execute() { message =

public getter methods. "Hello Struts 2!"; return SUCCESS; } public


String getMessage() { return message; }}

This Action class prepares the "Hello Struts 2!" message for display.
The `hello.jsp` View Page
The JSP (JavaServer Page) is the view component that displays dynamic content generated by the Action.

Displaying Content
<%@ page contentType="text/html; charset=UTF-8" %><%@ taglib prefix="s"
Using Struts 2 tags, specifically <s:property>, you can access properties from the Value Stack, uri="/struts-tags" %><html><head> <title>Hello Struts 2</title></head><body>
which typically contains data from your Action class. <h1><s:property value="message"/></h1></body></html>

The value="message" attribute directly references the getMessage() method in your HelloWorldAction.
Running the Application

Deploy to Container
Build WAR File
Copy the generated .war file (e.g., yourApp.war)
Open your terminal or command prompt in the from your target/ directory into the webapps/ folder
project's root directory and run: mvn clean package. of a Servlet Container like Apache Tomcat 9/10.
This compiles your code and packages it into a .war
file.

Verify Output
Access URL
You should see a web page displaying "Hello Struts
Start your Tomcat server. Then, open your web 2!", confirming your basic Struts 2 application is
browser and navigate to: running successfully.
http://localhost:8080/yourApp/hello.action
(replace yourApp with your project's artifact ID).
Conclusion and Next Steps
Recap Further Exploration

• We've successfully created a basic "Hello World" Interceptors: Learn how to apply cross-cutting concerns
application using Apache Struts 2, covering project like logging and security.
setup, configuration, and key components. Validation: Implement robust input validation for forms.
• This simple example provides a foundation for Internationalisation: Support multiple languages in
building more complex web applications. your application.

AJAX Integration: Explore asynchronous


communication for dynamic user experiences.

Official Documentation: Consult the Apache Struts 2


documentation for in-depth knowledge and advanced
topics.

Apache Struts 2 is a powerful framework that offers a wealth of features for developing enterprise-grade web
applications. Continue learning to unlock its full potential.

You might also like