Creating First Struts 2 Application

Creating First Struts 2 Application

In this article, I am going to discuss Creating First Struts 2 Application. Please read our previous article where we discussed the steps to create a Struts 2 Application. At the end of this article, you will understand the following pointers in detail.

  1. Create a Dynamic Web Project
  2. Create an Action Class
  3. Create a View
  4. Create the Main Page
  5. Configuration Files
  6. Enable Detailed Log
  7. Procedure for Executing the Application
Create A Dynamic Web Project

Open your Eclipse IDE. Navigate to File > New > Dynamic Web Project and then enter your project name (for example – HelloWorldStruts2) and then follow the rest of the options as shown in the below image.

Create A Dynamic Web Project using Structs

On the next screen select all the default options including the Generate Web.xml deployment descriptor option. Finally, it will create a dynamic web project into Eclipse IDE. To see the project window, navigate to Windows -> Show View -> Project Explorer. It will show like below image.

Creating First Struts 2 Application

From struts 2 lib folder C:\struts-2.2.3\lib copy the below files to our project’s WEB-INF\lib folder. Simply drag and drop all the files into the WEB-INF\lib folder.

  1. commons-fileupload-x.y.z.jar
  2. commons-io-x.y.z.jar
  3. commons-lang-x.y.jar
  4. commons-logging-x.y.z.jar
  5. commons-logging-api-x.y.jar
  6. freemarker-x.y.z.jar
  7. javassist-.xy.z.GA
  8. ognl-x.y.z.jar
  9. struts2-core-x.y.z.jar
  10. xwork-core.x.y.z.jar
Create an Action Class

We need Action class most of the time to implement business logic. Let’s create a java file and name it as Under the Java Resources -> src which is having a package named it as and add the contents written below to it.

When a user clicks on an URL the Action class gives a response to it. One or more than one method of Action class is executed and as a result, it returns a string. A JSP page is viewed based on the result value.

public class HelloWorldAction
    private String name;

    public String execute () throws Exception
        return "success";

    public String getName ()
        return name;

    public void setName (String name)
    { = name;

Let us create a simple class with a single property named “name”. There are standard getter and setter methods for the property named “name” and execute() method to return the string “success”.

An object of the HelloWorldAction class is created by the Struts 2 framework and it calls the execute() method to give a response to the user`s action. Inside the method, which finally returns the String constant put your business logic. In particular, for each URL, you have to implement one action class and either directly uses that class name as your action name or using struts.xml map it to some other name as shown below.

Create A View

We need to create a JSP page that will display the final message and be called by the Struts 2 framework when a predefined action takes place and this mapping is defined in the struts.xml file.

Now under the WebContent folder of your eclipse project create a JSP file HelloWorld.jsp. For this, right-click on the WebContent folder in the project explorer and go to new -> JSP File.

<%@ page contentType = "text/html; charset = UTF-8" %>
<%@ taglib prefix = "s" uri = "/struts-tags" %>

      <title>Hello World</title>
      Hello World, <s:property value = "name"/>

The job of taglib directive is to inform the Servlet container about the page usage by Struts 2 tags and also that these tags are going to be preceded by s. The s:property tag shows the value of the action class property named “name” that is returned by the method getName() of the HelloWorldAction class.

Create the Main Page

Under the WebContent folder create an index.jsp file. The JSP file will work as an initial action URL, for this reason, the user can easily inform the Struts 2 framework to invoke a defined method of the HelloWorldAction class while clicking and showing the HelloWorld.jsp view.

<%@ page language = "java" contentType = "text/html; charset = ISO-8859-1"
   pageEncoding = "ISO-8859-1"%>
<%@ taglib prefix = "s" uri = "/struts-tags"%>
   <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 

      <title>Hello World</title>
      <h1>Hello World From Struts2</h1>
      <form action = "hello">
         <label for = "name">Please enter your name</label><br/>
         <input type = "text" name = "name"/>
         <input type = "submit" value = "Say Hello"/>

blankIn the above view file, the hello action is defined. It will be mapped to the HelloWorldAction class and its execute() method using the struts.xml file. Whenever a user clicks on the Submit button the Struts 2 framework will run the execute() method that is defined in the HelloWorldAction class. On the basis of the returned value by the method, an exact view will be selected and made as a response.

Configuration Files

We must see the URL, the HelloWorldAction class (Model), and the HelloWorld.jsp (the view) and define the Struts 2 framework, which class will handle the user’s action (the URL) and what are the views of the method that are locating on the String resulting the method returns.

So, let’s generate a file called struts.xml. Since Struts 2 needs struts.xml to be present in the classes folder. Hence, for creating struts.xml file under the WebContent/WEB-INF/classes folder. By default, Eclipse does not create the “classes” folder, hence you have to do this yourself. For doing this, right-click on the WEB-INF folder in the project explorer and select New > Folder. Your struts.xml should be as follows:

<?xml version = "1.0" Encoding = "UTF-8"?>
   "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"

   <constant name = "struts.devMode" value = "true" />
   <package name = "helloworld" extends = "struts-default">
     <action name = "hello" 
         class = "" 
         method = "execute">
         <result name = "success">/HelloWorld.jsp</result>

blankFew words must be understood regarding the above configuration file. Here, we have to put down the constant struts.devMode to be accurate, because we are functioning in a development field and for this, we have to grasp useful log word. And after that, we have to explain a package known as HelloWorld.

Creating a package is helpful when you must group your actions together. In this example, we named it “hello” which is denoting the URL /hello.action and is supported by theHelloWorldAction.class. The execute() method of HelloWorldAction.class runs whenever the URL /hello.action is called. If the final result of the executes method returns “success”, then only we have to take the user to HelloWorld.jsp.

The next step is to generate a web.xml file which is an entry point for any request to Struts 2. Struts2 application will be a filter that explains a deployment descriptor (web.xml). So, we have to define an entry of org.apache.struts2.dispatcher.FilterDispatcher class in web.xml. The web.xml file must be created under the WEB-INF folder under WebContent. Eclipse had already made a skeleton web.xml file for your convenience when you created the project. So, let’s just do some modification as follows −

<?xml version = "1.0" Encoding = "UTF-8"?>
<web-app xmlns:xsi = ""
   xmlns = "" 
   xmlns:web = ""
   xsi:schemaLocation = ""
   id = "WebApp_ID" version = "3.0">
   <display-name>Struts 2</display-name>

Enable Detailed Log

Under WEB-INF/classes folder we can create a “” file to enable the complete logging functionality while using Struts 2. Put the below two lines in your property file.

org.apache.catalina.core.ContainerBase.[Catalina].level = INFO
org.apache.catalina.core.ContainerBase.[Catalina].handlers = \

Default define a ConsoleHandler for routing logging to stdout and also a FileHandler. We can set the threshold level of a handler’s log using SEVERE, WARNING, INFO, CONFIG, FINE, FINER, FINEST, or ALL. Finally, we are able to run the Hello World application using the Struts 2 framework.

Procedure for Executing the Application

To create a War file right click on the project name and go to Export -> WAR File. In the Tomcat’s webapps directory deploy that WAR file. Now start the Tomcat server and access the URL http://localhost:8080/HelloWorldStruts2/index.jsp. It will display the following screen:

Procedure for Executing the Application

For example, if you enter a value “Struts2” and submit the page. It will take you to the next page given below.

Creating First Struts 2 Application

Note: You can define index as an action in the struts.xml file and for this case, you can call the index page as http://localhost:8080/HelloWorldStruts2/index.action.

In the next article, I am going to discuss How to Create a struts 2 application in MyEclipse. Here, in this article, I try to explain Creating First Struts 2 Application and I hope you enjoy this Creating First Struts 2 Application article.

Leave a Reply

Your email address will not be published. Required fields are marked *