by Dinesh Thakur

As you learn a user sends a request from its web browser which is in the form of a URL. The request sent by the user passes through a standard filter chain which actually collected by the Controller (Filter Dispatcher). Controller examines each incoming URL request and determines which action should handle the request. Then the Controller transfers the request to the action class. The action plays an important role in the transfer of data from the request through to the view. Finally, the Result renders the output to the Web browser that requested.

In this Struts2 Example, we will discuss how to create a Struts2 Hello World Example in eclipse. The aim of this Example is to build a Struts2 Hello World web application that displays " You have been successfully executed Struts2 Hello World Example ". I am going to creating Dynamic Web Project with the help of eclipse IDE. The following files are required to create a Struts2 Hello World Example.

  1. web.xml
  2. struts.xml
  3. HelloWorld.java
  4. default.jsp
  5. HelloWorld.jsp

Create a Dynamic Web Project:

Click your Eclipse IDE and then go with File > New > Dynamic Web Project and enter project name as Struts2HelloWorldExample as given in the following screen:

           Struts2 HelloWorld Example

Click on OK button and you will see your Eclipse IDE project window something looks like as below:

           Struts 2 HelloWorld Example

In the Next Step you can copy the JAR files required by Struts2 framework from folder C:\ struts-2.3.16.3\lib to our project's WEB-INF\lib folder.

•   commons-fileupload-1.3

•   commons-io-2.0.1

•   commons-lang3-3.1

•   commons-logging-1.1.3

•   freemarker-2.3.19

•   javassist-3.11.0.GA

•   ognl-3.0.6

•   struts2-core-2.3.15.1

•   xwork-core-2.3.15.1

Create Web.xml (Configuration Files):

The entry point for any request to Struts2 web application is the web.xml. It’s also known as Deployment Descriptor (web.xml). The incoming request to the Struts2 application will be handled by StrutsPrepareAndExecuteFilter class for processing. In Struts2 the StrutsPrepareAndExecuteFilter class file plays the role of a controller or filter. We added filter file StrutsPrepareAndExecuteFilter in web.xml. The web.xml is created under the WEB-INF folder under WebContent.

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name> Struts2HelloWorldExample </display-name>
  <welcome-file-list>
     <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <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>

Create Default page:

After the processing of Web.xml, the control is transfer to default.jsp file in the WebContent folder. Default.jsp will serve as the initial action. When the user clicks on the link "Hello World Example" the Struts2 framework call the HelloWorld class file.

<%@ 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" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
         <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
         <title>Struts2 Hello World Example</title>
    </head>
      <body>
        <p><h2><font color="red">Struts2 Hello World Example </font></h2>
        <p><a href="/<s:url value="/Struts2HelloWorldExample/HelloWorld"  />" target="_blank">Hello World Example</a>
      </body>
</html>

Configuration Files (struts.xml)

The framework handles all of the controller work and only needs to be informed about which request URL maps to which action. Then this information is passed to the framework using an XML configuration file i.e. struts.xml. It is another Configuration file apart from web.xml.

In display.jsp one link "Hello World Example" is present. Clicking on the link will fire an action that is Displnterface and then it goes to the struts.xml. So let's our next step is to create a file called struts.xml inside the WebContent/WEB-INF/classes folder.

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
            "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
            "http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
    <constant name="struts.enable.DynamicMethodInvocation" value="false" />
    <constant name="struts.devMode" value="true" />
    <package name="default" namespace="/" extends="struts-default">
                        <action name="HelloWorld" class="com.ecomputernotes.HelloWorld">
                          <result name="success">/HelloWorld.jsp</result>
                        </action>
    </package>
</struts>

Create Action Class:

Actions are the core of the Struts 2 framework, as they are for any MVC (Model View Controller) framework. Each URL is mapped to a specific action, which provides the processing logic necessary to service the request from the user. This is an action-oriented framework.

But the action also serves in two other important capacities. The action plays an important role in the transfer of data from the request through to the view, whether it is a JSP or other type of result. The action must assist the framework in determining which result should render the view that will be returned in the response to the request.

So let us we create a java file HelloWorld.java under src folder with a package name com.ecomputernotes with the contents given below.

package com.ecomputernotes;
import com.opensymphony.xwork2.ActionSupport;
public class HelloWorld extends ActionSupport {
  private static final long serialVersionUID = 1L;
     public String execute() throws Exception {
  return SUCCESS;
   }
}

Create a View

So, finally we need to create a View page of JSP to present the final message, this page will be called when a action will happen and this mapping will be defined in struts.xml file. So let us we create the HelloWorld.jsp in the WebContent folder with the contents given below.

<%@ 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" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
       <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
       <title>Struts2 Hello World Example</title>
    </head>
       <body>
         <h2><font color="red">You have been successfully executed Struts2 Hello World Example</font></h2>
         <a href="/Struts2HelloWorldExample/">Go to Default Page</a>
        </body>
</html>

Execute the Application

After running the Application in Eclipse IDE http://localhost:8080/ Struts2HelloWorldExample/ This will give you following screen:

After clicking the link "Hello World Example". You should see the final message.

         Struts 2 Examples