Struts Tutorial for Beginners

Filed Under: Struts 2

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="" xmlns:xsi=""


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 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=""


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.


<%@ page language="java" contentType="text/html; charset=US-ASCII"
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "">
<%-- 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.


<%@ 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" "">
<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.


<%@ 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" "">
<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.

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) { = 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.


<?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

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.


  1. ashok says:



    i added tag lib to but i still face error in jsp tag lib

  2. baswaraj says:

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

  3. Hemanth says:

    Your way of explaining is awesome.

  4. prashant says:

    Work perfectly.
    Thanks for tutorial.

  5. Sunil says:

    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.

    1. SandeepGS says:

      In web.xml try to add welcomefile as login.jsp…I also got the same issue but I resolved it by adding like wise.

      Thank You!
      G Sandeep

  6. Balwinder says:

    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
    With regards
    balwinder singh

    looking forward to ur positive response

  7. Rais says:

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

  8. vikash says:

    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.

  9. niraj says:

    i have also get

    SEVERE: Exception starting filter struts2
    at org.apache.catalina.loader.WebappClassLoader.loadClass(
    at org.apache.catalina.loader.WebappClassLoader.loadClass(
    at org.apache.catalina.core.DefaultInstanceManager.loadClass(
    at org.apache.catalina.core.DefaultInstanceManager.loadClassMaybePrivileged(
    at org.apache.catalina.core.DefaultInstanceManager.newInstance(
    at org.apache.catalina.core.ApplicationFilterConfig.getFilter(
    at org.apache.catalina.core.ApplicationFilterConfig.(
    at org.apache.catalina.core.StandardContext.filterStart(
    at org.apache.catalina.core.StandardContext.startInternal(
    at org.apache.catalina.util.LifecycleBase.start(
    at org.apache.catalina.core.ContainerBase$
    at org.apache.catalina.core.ContainerBase$
    at Source)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$ Source)
    at Source)…
    plz help me..

    1. Soo says:

      Check the version of struts2-core.jar.
      Try to use higher version of the jar.
      You can download it from

  10. niraj says:

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

  11. niraj says:

    i also send the error snapshot on your email (pankaj.0323@gmail) plz check.. thnks in advance.

  12. niraj says:

    when ever i convert my project to maven then give error about to create pom file…and how to i get maven dependency library file…

  13. niraj says:

    i try to configure my first struts 2 project by your given style but it’s not configure can you tell how to create and configure my struts 2 project…..please give every basic step from start to end to do it..

    1. Pankaj says:

      I have already listed all the steps, from start to end. There is nothing more to it, check if u missed some configurations? And what is the issue you are facing?

      1. egalt says:

        Yeah, you did a great job. Very clear and concise. Nice work and explanation!

  14. Rahul Singh prac says:

    Hi Pankaj,

    Thanks for these lovely tutorials. I am trying to get into programming and I have chosen Java as my platform.
    Kindly let me know what are the best practices and approach I should follow to achieve the same.
    Looking forward for you reply.


Comments are closed.

Generic selectors
Exact matches only
Search in title
Search in content