Software Training Institute in Chennai with 100% Placements – SLA Institute

Easy way to IT Job

Share on your Social Media

Struts Tutorial for Beginners

Published On: August 11, 2025

Struts Tutorial for Beginners

Are you ready to embark on a journey into the world of web development with a powerful framework?  Apache Struts 2 is a powerful, elegant framework for building enterprise-level Java web applications. This tutorial is designed to take you through all the steps necessary to get started, from building your environment to building your application. Take a look at our Struts course syllabus below for a comprehensive learning path, if you are serious about mastering this technology.

What is Struts 2 and Why Use It?

Apache Struts 2 is an open-source, MVC (Model-View-Controller) framework for developing Java web applications. It operates a request-driven architecture, allowing developers to built and maintain complex web applications.

Key Benefits of Using Struts 2:

  • MVC Architecture: It divides the application functionality into three sections Model, View, and Controller, making development and maintenance easier.
  • Convention Over Configuration: Many things need not be configured when using, things get configured automatically, you can now spend a little less time writing xml.
  • Plugin Architecture: Struts 2 is powerful and easily extensible and expandable using plugins, for example, REST services, Spring, etc.
  • Interceptor-Based Architecture: Struts 2 interceptors is a powerful center point of the framework which allows you to pre and post process your request for a I/O type operations like logging, validation, securing things etc.

Setting Up your Development Environment

Before we can start writing code, we have to set up our tools. This tutorial assumes that you have the following installed:

  1. JDK (Java Development Kit): Version 8 or higher.
  2. Apache Maven: A build automation tool for Java projects.
  3. You need an IDE (Integrated Development Environment): Eclipse, IntelliJ IDEA, or NetBeans. We will use Eclipse in this tutorial. 
  4. A servlet container: Apache Tomcat is the most popular and easiest to get running.
Setting Up a Maven Project in Eclipse
  • Open Eclipse and go to File -> New -> Maven Project.
  • Check Create a simple project (skip archetype selection) and then select Next.
  • Fill in the following details:
    • Group Id: com.example
    • Artifact Id: my-first-struts-app
    • Packaging: war
  • Click Finish. 
Configuring Struts 2 with Maven

Maven takes care of the dependency management for us. Now we just have to add the Struts 2 core dependency to our pom.xml file.

XML

<project xmlns=”http://maven.apache.org/POM/4.0.0″ …>

  …

  <dependencies>

    <dependency>

      <groupId>org.apache.struts</groupId>

      <artifactId>struts2-core</artifactId>

      <version>2.5.30</version>

    </dependency>

    <dependency>

      <groupId>javax.servlet</groupId>

      <artifactId>javax.servlet-api</artifactId>

      <version>3.1.0</version>

      <scope>provided</scope>

    </dependency>

  </dependencies>

  …

</project>

Note: Always use the newest stable build of Struts 2. Check out our Struts Course Online for further learning.

The web.xml Configuration

The web.xml is the deployment descriptor for your web app. We will configure the Struts 2 Filter Dispatcher to process all incoming requests.

Create a web.xml file inside src/main/webapp/WEB-INF/.

XML

<?xml version=”1.0″ encoding=”UTF-8″?>

<web-app xmlns=”http://xmlns.jcp.org/xml/ns/javaee”

         xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”

         xsi:schemaLocation=”http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd”

         version=”3.1″>

    <filter>

        <filter-name>struts2</filter-name>

    <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>

    </filter>

    <filter-mapping>

        <filter-name>struts2</filter-name>

        <url-pattern>/*</url-pattern>

    </filter-mapping>

</web-app>

The StrutsPrepareAndExecuteFilter is the core of Struts 2. It intercepts all requests (/*) and hands it to the framework. 

Building Your First Struts 2 Action

The Action class is the “Controller” of the MVC pattern, processes the user’s request, and prepares the data for the view. 

We will create a simple HelloWorldAction.

src/main/java/com/example/action/HelloWorldAction.java

Java

package com.example.action;

import com.opensymphony.xwork2.ActionSupport;

public class HelloWorldAction extends ActionSupport {

    private String message;

    public String getMessage() {

        return message;

    }

    public String execute() {

        message = “Hello, Struts 2!”;

        return SUCCESS;

    }

}

  • We extend ActionSupport so that we can have access to useful methods and constants such as SUCCESS. 
  • The execute() method is the default method that Struts 2 will run. 
  • The message property will be used to pass data to the view. 

Recommended: Java Course Online.

The struts.xml Configuration

This is the main configuration for Struts 2. It maps incoming requests to our Action classes.

Create a struts.xml file inside src/main/resources/.

XML

<?xml version=”1.0″ encoding=”UTF-8″?>

<!DOCTYPE struts PUBLIC

    “-//Apache Software Foundation//DTD Struts Configuration 2.5//EN”

    “http://struts.apache.org/dtds/struts-2.5.dtd”>

<struts>

    <package name=”default” namespace=”/” extends=”struts-default”>

        <action name=”hello” class=”com.example.action.HelloWorldAction”>

            <result name=”success”>/WEB-INF/pages/hello.jsp</result>

        </action>

    </package>

</struts>

  • <package>: This is a logical grouping of actions, and extending=”struts-default” gives us access to a lot of built-in features.
  • <action>: This maps the requested URL of (/hello.action) to our HelloWorldAction class.
  • <result>: This tells what happens after the action method returns SUCCESS, and tells the Struts framework to forward them to hello.jsp.

Creating the View (JSP Page)

The View is responsible to display data. We will use a JSP (JavaServer Page) to display the message from our Action.

Create a hello.jsp file under src/main/webapp/WEB-INF/pages/.

Java

<%@ page language=”java” contentType=”text/html; charset=UTF-8″ pageEncoding=”UTF-8″%>

<%@ taglib prefix=”s” uri=”/struts-tags” %>

<!DOCTYPE html>

<html>

<head>

    <meta charset=”UTF-8″>

    <title>Hello Struts 2</title>

</head>

<body>

    <h1><s:property value=”message”/></h1>

</body>

</html>

  • <%@ taglib prefix=”s” uri=”/struts-tags” %>: This is essential for using Struts 2 tags.
  • <s:property value=”message”/>: This tag gets the value of the message property from our Action class, and displays it on the page.

Running Your Application

Now we are ready to run our application on our Tomcat server.

  1. Right-click your project in Eclipse and choose Run As -> Run on Server.
  2. Select your configured Tomcat server and click Finish.
  3. Go to a web browser and go to: http://localhost:8080/my-first-struts-app/hello.action.

You should see a page that displays “Hello, Struts 2!”.

Key Struts 2 Concepts

As you develop more sophisticated applications you will become more familiar with these key concepts: 

  • Value Stack and OGNL: Struts 2 utilizes an advanced capability called the Value Stack to represent objects and properties to the view. The expression language that provides access to those objects is OGNL (Object-Graph Navigation Language). For example,  demonstrates OGNL usage to get the message property.
  • Interceptors: Interceptors are objects capable of pre-processing or post-processing on the incoming request. Interceptors are defined in struts.xml and are useful for cross-cutting concerns, such as: security, validation, and logging.
  • Validation: The Struts 2 framework has built-in validation, and can actually perform validation of the input form on the server-side. You can provide validation definitions using either annotated or XML based configurations.
  • Form tags: Struts 2 allows you to use a series of form tags (e.g. , ) that removes much of the boilerplate, allowing the developer to ease the burden of creating a form.

Explore: All Software Training Courses.

Conclusion

Now, you have demonstrated the ability to construct your first Struts 2 web application. As with anything, the best way to learn how to use Struts 2 effectively is through experience, and for anyone looking to learn Struts 2 to build professional applications may require a curriculum. We have provided a complete Struts 2 Course in Chennai that provides complete lessons and hands on coding assignments and projects along with support from an instructor to assist you in learning how to be a Java web developer.

Share on your Social Media

Just a minute!

If you have any questions that you did not find answers for, our counsellors are here to answer them. You can get all your queries answered before deciding to join SLA and move your career forward.

We are excited to get started with you

Give us your information and we will arange for a free call (at your convenience) with one of our counsellors. You can get all your queries answered before deciding to join SLA and move your career forward.