Tutorial

Struts Tutorial for Beginners

Published on August 4, 2022
author

Pankaj

Struts Tutorial for Beginners

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.

pom.xml

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">
	<modelVersion>4.0.0</modelVersion>
	<groupId>Struts2XMLHelloWorld</groupId>
	<artifactId>Struts2XMLHelloWorld</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>war</packaging>

	<dependencies>
		<dependency>
			<groupId>org.apache.struts</groupId>
			<artifactId>struts2-core</artifactId>
			<version>2.3.15.1</version>
		</dependency>
	</dependencies>
	<build>
		<sourceDirectory>src</sourceDirectory>
		<plugins>
			<plugin>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.1</version>
				<configuration>
					<source>1.6</source>
					<target>1.6</target>
				</configuration>
			</plugin>
			<plugin>
				<artifactId>maven-war-plugin</artifactId>
				<version>2.3</version>
				<configuration>
					<warSourceDirectory>WebContent</warSourceDirectory>
					<failOnMissingWebXml>false</failOnMissingWebXml>
				</configuration>
			</plugin>
		</plugins>
		<finalName>${project.artifactId}</finalName>
	</build>
</project>

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 2.3.15.1 (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"
	xmlns="https://java.sun.com/xml/ns/javaee"
	xsi:schemaLocation="https://java.sun.com/xml/ns/javaee https://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
	version="3.0">
	<display-name>Struts2XMLHelloWorld</display-name>
	
	<filter>
		<filter-name>struts2</filter-name>
		<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
	</filter>

	<filter-mapping>
		<filter-name>struts2</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
</web-app>

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"
    pageEncoding="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"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>Login Page</title>
</head>
<body>
<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>
</s:form>
</body>
</html>

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"
    pageEncoding="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">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>Welcome Page</title>
</head>
<body>
<h3>Welcome <s:property value="name"></s:property></h3>
</body>
</html>

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"
    pageEncoding="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">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>Error Page</title>
</head>
<body>
<h4>User Name or Password is wrong</h4>
<s:include value="login.jsp"></s:include>
</body>
</html>

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 {
	
	@Override
	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"?>

<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
	"https://struts.apache.org/dtds/struts-2.3.dtd">
<struts>

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

</package>

</struts>

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.

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the authors
Default avatar
Pankaj

author

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.

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
JournalDev
DigitalOcean Employee
DigitalOcean Employee badge
November 20, 2019

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

- ashok

    JournalDev
    DigitalOcean Employee
    DigitalOcean Employee badge
    July 27, 2018

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

    - baswaraj

      JournalDev
      DigitalOcean Employee
      DigitalOcean Employee badge
      May 25, 2018

      Your way of explaining is awesome.

      - Hemanth

        JournalDev
        DigitalOcean Employee
        DigitalOcean Employee badge
        April 11, 2016

        Work perfectly. Thanks for tutorial.

        - prashant

          JournalDev
          DigitalOcean Employee
          DigitalOcean Employee badge
          March 16, 2016

          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

            JournalDev
            DigitalOcean Employee
            DigitalOcean Employee badge
            November 11, 2015

            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

              JournalDev
              DigitalOcean Employee
              DigitalOcean Employee badge
              July 13, 2015

              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

                JournalDev
                DigitalOcean Employee
                DigitalOcean Employee badge
                February 23, 2015

                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

                  JournalDev
                  DigitalOcean Employee
                  DigitalOcean Employee badge
                  December 20, 2014

                  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

                    JournalDev
                    DigitalOcean Employee
                    DigitalOcean Employee badge
                    December 19, 2014

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

                    - niraj

                      Try DigitalOcean for free

                      Click below to sign up and get $200 of credit to try our products over 60 days!

                      Sign up

                      Join the Tech Talk
                      Success! Thank you! Please check your email for further details.

                      Please complete your information!

                      Become a contributor for community

                      Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

                      DigitalOcean Documentation

                      Full documentation for every DigitalOcean product.

                      Resources for startups and SMBs

                      The Wave has everything you need to know about building a business, from raising funding to marketing your product.

                      Get our newsletter

                      Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

                      New accounts only. By submitting your email you agree to our Privacy Policy

                      The developer cloud

                      Scale up as you grow — whether you're running one virtual machine or ten thousand.

                      Get started for free

                      Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

                      *This promotional offer applies to new accounts only.