Home Struts Struts2.1.8 Struts 2.1.8 Hello World Example


Share on Google+Share on Google+

Struts 2.1.8 Hello World Example

Learn how to develop Hello World application in Struts 2.

Struts 2.1.8 Hello World Example


In this section we will learn how to develop our first Hello World example using the latest Struts 2.8.1. We will use the properties files to internalize the application.

After completing this example you will be able to develop simple Hello World example in Struts 2.8.1. You will also learn how to configure struts environment in web.xml file. We have provided the necessary library files in the example download. You can download the example and run it on any Servlet container. But we highly recommend you to use Tomcat 6 to run the example. The Ant build tool is also needed to compile the java source code. You can use any text editor to write java and jsp code.

So, let's get started with the Hello World tutorial using Struts 2.1.8:

The web configuration file web.xml:

In the web.xml file we are required to configure the Struts filter. It is done by adding the following code to web.xml file:


and then configure the filter mapping for struts2 filter:


The complete code of web.xml file is:

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

<display-name>RoseIndia.net Struts 2.8.1 Tutorials</display-name>





Writing the action class:

The action class is developed by the programmer and business processing logic is written in the Action class. The Struts framework executes the default (or specified) method in the action class.

Here is the code of our HelloWorld.java file:

package net.roseindia;

import com.opensymphony.xwork2.ActionSupport;

public class HelloWorld extends ActionSupport {

  public String execute() throws Exception {
  return SUCCESS;

 * Provide default valuie for Message property.
  public static final String MESSAGE = "HelloWorld.message";

 * Field for Message property.
  private String message;

 * Return Message property.
 @return Message property
  public String getMessage() {
  return message;

 * Set Message property.
 @param message Text to display on HelloWorld page.
  public void setMessage(String message) {
  this.message = message;

The above class extends com.opensymphony.xwork2.ActionSupport class. It provides a method getText(), which retrieves the message for the current local from the message resource file. The message resource file(s)  must present in the same package. In this application two message resource files package.properties and package_es.properties are present in the net.roseindia package.

The Struts framework executes the following default method :

  public String execute() throws Exception {
  return SUCCESS;

This method is just setting the message and the returning the SUCCESS value, which is the result of the action. It is then matched in the action declaration in the struts.xml file and appropriate view is generated and displayed to the user. In this Hello World example HelloWorld.jsp is rendered and displayed to the user. Here is configuration of the HelloWorld action defined in struts.xml file:

<action name="HelloWorld" class="net.roseindia.HelloWorld">

Here is the complete struts.xml file:

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


<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="false" />

<package name="example" namespace="/roseindia" extends="struts-default">

<action name="HelloWorld" class="net.roseindia.HelloWorld">

<!-- Add actions here -->

<!-- Add packages here -->


The view of the application:

The HelloWorld.jsp file is view in the Hello World example. Here is the code of HelloWorld.jsp file:

<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
  <title><s:text name="HelloWorld.message"/></title>
   <h2><s:property value="message"/></h2>
<a href="/struts2/">Back to Index page</a>

In the jsp file <s:property value="message"/> is used to display the message from the action class and <s:text name="HelloWorld.message"/>  is used to display the message from the properties file.

Compiling the example:

You can download the complete example from here and then extract and place the web application on tomcat. To compile the application go to command prompt and type ant as shown below:


Buildfile: build.xml

[delete] Deleting directory C:\TutorialsWriting\Struts2\apache-tomcat-6.0.29\
[mkdir] Created dir: C:\TutorialsWriting\Struts2\apache-tomcat-6.0.29\webapp

[copy] Copying 1 file to C:\TutorialsWriting\Struts2\apache-tomcat-6.0.29\w

[copy] Copying 2 files to C:\TutorialsWriting\Struts2\apache-tomcat-6.0.29\

[javac] Compiling 1 source file to C:\TutorialsWriting\Struts2\apache-tomcat
[jar] Building jar: C:\TutorialsWriting\Struts2\apache-tomcat-6.0.29\webap



Total time: 1 second

Running example:

Start tomcat and type following urls to run the example:



In this section we have studied how to make Hello World application in Struts 2.1.8 framework.

Download code tutorial code.



If you enjoyed this post then why not add us on Google+? Add us to your Circles

Liked it!  Share this Tutorial

Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: August 9, 2010

Related Tutorials

Ask Questions?    Discuss: Struts 2.1.8 Hello World Example  

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image