// Tutorial //

Struts Tutorial for Beginners

Published on August 3, 2022
Default avatar
By Pankaj
Developer and author at DigitalOcean.
Struts Tutorial for Beginners

While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.

Welcome to Struts Tutorial for Beginners. Struts is one of the oldest frameworks to build Java Web Application.

Struts Tutorial

struts tutorial, struts 2, struts 2 tutorial, struts2 Struts was the initial implementation of MVC design pattern and it has evolved a lot along with latest enhancements in Java, Java EE technologies. Struts tutorial article is aimed to provide basic details of Struts 2 and how we can create our first “Hello World” Struts 2 application.

Struts 2

Apache Struts 2 is an open source, industry standard, flexible and extendable framework to build Java EE web application. Struts 2 is based on OpenSymphony WebWork framework. Struts 2 is very flexible in terms of development and configurations and we will see how easy it is to develop a web application using Struts 2 framework.

Struts 2 Architecture Diagram

Below diagram shows different component of Struts 2 in a web application. Struts 2 Architecture Diagram

Struts 2 Interceptors

Struts Interceptors are like Servlet Filters that executes before and after the request is being processed. They are used to perform common operations for different actions. For example logging, session validation, adding common headers to response etc.

Struts 2 ValueStack and OGNL

ValueStack is the storage area where the application data is stored by Struts 2 for processing a client request. The data is stored in ActionContext objects that use ThreadLocal to have values specific to the particular request thread. Object-Graph Navigation Language (OGNL) is a powerful Expression Language that is used to manipulate data stored on the ValueStack. As you can see in architecture diagram, both interceptors and result pages can access data stored on ValueStack using OGNL.

Struts 2 Action

Struts 2 Action components handle the client requests. Struts 2 provides different ways to create Action classes.

  1. By implementing com.opensymphony.xwork2.Action interface.
  2. By extending com.opensymphony.xwork2.ActionSupport class. Usually it’s used to create empty action classes to forward request to another resource.
  3. Annotating a class with @Action or @Actions annotation.
  4. Following naming convention for classes, name should end with Action and should have execute() method.

Struts 2 Result

Result components are usually JSP or HTML pages to create view for client response. Struts 2 provides their own tags that we can use in JSP pages to create response. Struts tags are great example of JSP Custom Tags.

Struts 2 Declarative Architecture and Wiring

Struts 2 provides two ways to configure our application for action classes and result pages.

  1. Struts XML File: We have struts.xml file in WEB-INF/classes directory where we can configure our application action classes and result pages.
  2. Annotation: We can use Java Annotations to provide metadata information about a class. Struts 2 convention plugin can be used to annotate java classes with @Action and @Result annotations to create configure action classes and associated result pages.

Whichever way we use to configure our application, the end result will always be the same.

Struts Tutorial - Hello World XML Based Application

Let’s see how we can create our first Struts 2 Hello World application. First of all we need is Struts 2 jar files, the easiest way is to download it from Struts 2 Official Downloads page. But when you will check out the libs in the downloaded archive, you will see a lot of jar files that we don’t need for our simple application. So I will create a maven project and add struts-core dependency only, all the other transitive dependency jars will be automatically downloaded and added to the application. Our final project structure will be like below image. Struts tutorial, struts 2 Hello World Create a new Dynamic Web Project Struts2XMLHelloWorld in Eclipse and then convert it to maven project like below image. Eclipse Convert Dynamic Web Application To Maven Project You will notice pom.xml file is added in the root directory of the project. Our project setup in Eclipse is ready, let’s look at the different components in order.


Open pom.xml file and add struts core dependency, the final pom.xml will look like below.

<project xmlns="https://maven.apache.org/POM/4.0.0" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="https://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">


Notice that I have overridden finalName element to avoid version number getting added in the WAR file when we do maven build. Other parts are added by Eclipse itself, the only dependency we need is struts2-core whose current version is (as of 10-Sep-2013). Just do maven build of the application and you will see a lot of jars added to the application lib directory and shown in Maven Dependencies section of the project like below image. Struts 2 maven dependencies, struts tutorial

Struts 2 web.xml configuration

We need to add org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter filter to the web application and provide the URL pattern where we want Struts to take care of the client request. Our web.xml looks like below;

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="https://java.sun.com/xml/ns/javaee https://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"


For Struts 2 version below 2.1.3, the filter-class was org.apache.struts2.dispatcher.FilterDispatcher.

Struts Tutorial - Result Pages

We have three JSP pages that will be used by the application, we are using Struts 2 tags to create our JSP pages. login.jsp

<%@ page language="java" contentType="text/html; charset=US-ASCII"
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "https://www.w3.org/TR/html4/loose.dtd">
<%-- Using Struts2 Tags in JSP --%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>Login Page</title>
<h3>Welcome User, please login below</h3>
<s:form action="login">
	<s:textfield name="name" label="User Name"></s:textfield>
	<s:textfield name="pwd" label="Password" type="password"></s:textfield>
	<s:submit value="Login"></s:submit>

Notice the form field names are name and pwd, we will see how they are used in Action classes. welcome.jsp

<%@ page language="java" contentType="text/html; charset=US-ASCII"
<%@ taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "https://www.w3.org/TR/html4/loose.dtd">
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>Welcome Page</title>
<h3>Welcome <s:property value="name"></s:property></h3>

Notice the struts tag s:property that we can use to get request attributes, the name is same as in login.jsp. error.jsp

<%@ page language="java" contentType="text/html; charset=US-ASCII"
<%@ taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "https://www.w3.org/TR/html4/loose.dtd">
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>Error Page</title>
<h4>User Name or Password is wrong</h4>
<s:include value="login.jsp"></s:include>

This is a simple JSP page where we are adding error message and including login page in response.

Struts Tutorial - Action Classes

Our application has only one Action class where we are implementing Struts 2 Action interface. LoginAction.java

package com.journaldev.struts2.action;

import com.opensymphony.xwork2.Action;

public class LoginAction implements Action {
	public String execute() throws Exception {
		if("pankaj".equals(getName()) && "admin".equals(getPwd()))
		return "SUCCESS";
		else return "ERROR";
	//Java Bean to hold the form parameters
	private String name;
	private String pwd;
	public String getName() {
		return name;
	public void setName(String name) {
		this.name = name;
	public String getPwd() {
		return pwd;
	public void setPwd(String pwd) {
		this.pwd = pwd;

Notice the action class is also a java bean with same variables as login.jsp and their getter and setter methods. Struts will take care of mapping the request parameters to the action class variables.

Struts Tutorial - Configuration File

Since we are using XML based configuration for wiring our application, we need to create Struts configuration file that should be named as struts.xml and inside WEB-INF/classes directory. struts.xml

<?xml version="1.0" encoding="UTF-8"?>

	"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"

<package name="user" namespace="/User" extends="struts-default">
	<action name="home">
	<action name="login" class="com.journaldev.struts2.action.LoginAction">
	<result name="SUCCESS">/welcome.jsp</result>
	<result name="ERROR">/error.jsp</result>



For action “home”, there is no Action class and only one result, so request will be forwarded to the login.jsp page. For action “login”, LoginAction is the action class and if execute() method returns “SUCCESS” the request will be processed by welcome.jsp and for “ERROR” it will be forwarded to error.jsp page. namespace=“/User” is important and used in URL to access the action classes, it’s provided to create different modules. So we can access our application with URL https://localhost:8080/Struts2XMLHelloWorld/User/home.action. Notice that URL is ending with .action that is the default suffix for Struts 2 action like it is .do for Struts 1.

Struts Tutorial - Struts 2 Hello World Test

When we run our application, we get following response pages. Struts tutorial Hello World XML login Struts tutorial Hello World XML Home Struts 2 tutorial Hello World XML Error

Download Struts2 Hello World Example Project

Thats all for Struts 2 beginners tutorial, check out next article we are using annotations to create Struts 2 Web Application without using struts.xml configuration file.

If you’ve enjoyed this tutorial and our broader community, consider checking out our DigitalOcean products which can also help you achieve your development goals.

Learn more here

About the authors
Default avatar


Developer and author at DigitalOcean.

Still looking for an answer?

Was this helpful?

hi… org.apache.struts struts-taglib 1.3.10 i added tag lib to but i still face error in jsp tag lib

- ashok

    sir u r explanation was awesome and easy to understand. can u add real time projects .

    - baswaraj

      Your way of explaining is awesome.

      - Hemanth

        Work perfectly. Thanks for tutorial.

        - prashant

          Hi Pankaj, I downloaded your project, I did not change anything. But I am getting following error when I am running this … " There is no Action mapped for namespace [/] and action name [User] associated with context path [/Struts2XMLHelloWorld]". Please help me, I am new to struts.

          - Sunil

            sir i’m a fresher and i have to prepare for interview with struts and hibernate can u tell upto what level should i do it Thanks With regards balwinder singh looking forward to ur positive response

            - Balwinder

              Sir please tell em about maven dependency , after using maven we dont need jar or api? how it is work please explain, thanx in advance

              - Rais

                I make a simple function login & logout on struts2 after logout i press back button go to loginsuccess page actually it will go to login page. Please give me some information to remove the error.

                - vikash

                  i have also get SEVERE: Exception starting filter struts2 java.lang.ClassNotFoundException: org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1714) at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1559) at org.apache.catalina.core.DefaultInstanceManager.loadClass(DefaultInstanceManager.java:527) at org.apache.catalina.core.DefaultInstanceManager.loadClassMaybePrivileged(DefaultInstanceManager.java:509) at org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:137) at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:260) at org.apache.catalina.core.ApplicationFilterConfig.(ApplicationFilterConfig.java:107) at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:4775) at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5452) at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559) at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549) at java.util.concurrent.FutureTask.run(Unknown Source) at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) at java.lang.Thread.run(Unknown Source)… plz help me…

                  - niraj

                    can you have a another way to create Struts project without help of maven…if yes then what is it.

                    - niraj