Basic Struts2 Tutorial

Lately I was surprised to see how outdated nearly all Struts2 books and tutorials have become. So, here we go! A tutorial based on Struts 2.3.x and Eclipse.

Downloading Struts 2 Library

Go to Struts download page.


Download the Struts library distribution as shown above. Unzip it somewhere. You will see all the JAR files in the lib folder.


Create Eclipse Project

Make sure that you have Eclipse IDE for Java EE Developers installed and Tomcat is configured there.

Create a dynamic web project called StrutsHelloWorld in Eclipse (File > New > Dynamic Web Project).

Add the following JAR files to the WEB-INF/lib folder.


Configure web.xml

Eclipse does not generate web.xml by default. Right click the project and select Java EE Tools > Generate Deployment Descriptor Stub.

Open WEB-INF/web.xml. Register Struts2 filter by adding these lines.



Save changes.

Develop the Action Class

We will develop a simple action class that will take user’s name as input and print out a greeting message.

Create a class called com.example.HelloWorld. Add the following code:

public class HelloWorld {
 private String message;
 private String userName;
 public String execute() {
  setMessage("Hello " + getUserName());
  return "ok";
 public String getMessage() {
  return message;
 public void setMessage(String message) {
  this.message = message;
 public String getUserName() {
  return userName;
 public void setUserName(String userName) {
  this.userName = userName;

Register the Action Class

Struts 2 configuration file is struts.xml and is located in the class path in the default package.


Create a file called struts.xml directly under the src folder.

Add these lines to the file:

<?xml version="1.0" encoding="UTF-8" ?>
 "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
 <package name="default" extends="struts-default">
  <action name="HelloWorld" class="com.example.HelloWorld">
   <result name="ok">/success.jsp</result>

Note, you must add the DOCTYPE. Otherwise, Struts 2 will fail to parse it at runtime.

Create the JSP Files

First, we will create the index.jsp page that will have a form where a user can enter her name.


In the WebContent folder, create a file called index.jsp. Add the following code:

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

 <s:form action="HelloWorld" >
 <s:textfield name="userName" label="User Name" />

Basically, this binds the input text field to the userName property of the action class.

Next, we will create success.jsp. The file is loaded when “ok” result is returned by the execute() method.

Create success.jsp and add this code:

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

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

This shows the message property of the action class.

Run the Application

Deploy the project in tomcat. Then run this URL:



Enter a name in the text box and submit the form.


Make sure that you see the message.


3 thoughts on “Basic Struts2 Tutorial

  1. Oh my goodness! Incredible article dude! Many thanks, However I am going through difficulties with your RSS.

    I don’t know the reason why I am unable to subscribe to it.
    Is there anybody else getting similar RSS problems?

    Anyone who knows the solution can you kindly respond? Thanks!!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s