Meyyappan has posted 101 posts at DZone. View Full User Profile

Struts Hello World Example in Eclipse

  • submit to reddit

In this tutorial you will learn how to create a Struts hello world application in eclipse. First create a new project, go to File->New and select DynamicWebProject.

Enter the project name and click the Finish button.

Add the following jar files to the WEB-INF\lib directory.

Right click the src folder and select New->Package.

Enter the package name as com.vaannila.form and click Finish.

Now right click the newly created package and select New->Class.

Enter the class name as HelloWorldForm and the superclass name as org.apache.struts.action.ActionForm and click Finish.

In the HelloWorldForm class add the following code.

package com.vaannila.form;

import org.apache.struts.action.ActionForm;

public class HelloWorldForm extends ActionForm {

    private static final long serialVersionUID = -473562596852452021L;

    private String message;

    public String getMessage() {
        return message;

    public void setMessage(String message) {
        this.message = message;

In the same way create a new package com.vaannila.action and create a HelloWorldAction class extending org.apache.struts.action.Action. Add the following code to the action class and save it.

package com.vaannila.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.vaannila.form.HelloWorldForm;

public class HelloWorldAction extends Action {

public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
    HelloWorldForm hwForm = (HelloWorldForm) form;
    hwForm.setMessage("Hello World");
    return mapping.findForward("success");

Here we typecast the ActionForm to HelloWorldForm and set the message value.

Add the following entries in the struts-config.xml file.

<?xml version="1.0" encoding="ISO-8859-1" ?>

<!DOCTYPE struts-config PUBLIC
          "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"


    	<form-bean name="helloWorldForm" type="com.vaannila.form.HelloWorldForm"/>

        <forward name="helloWorld" path="/"/>

        <action path="/helloWorld" type="com.vaannila.action.HelloWorldAction" name="helloWorldForm">
        	<forward name="success" path="/helloWorld.jsp" />


Now configure the deployment descriptor. Add the following configuration information in the web.xml file.

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="" xmlns="" xmlns:web="" xsi:schemaLocation="" id="WebApp_ID" version="2.5">




When we run the application the index.jsp page will be executed first. In the index.jsp page we redirect the request to the URI, which inturn invokes the HelloWorldAction.

<%@ taglib uri="" prefix="logic" %>
<logic:redirect forward="helloWorld"/>

In the action class we return the ActionForward "success" which is mapped to the helloWorld.jsp page. In the helloWorld.jsp page we display the "Hello World" message.

<%@taglib uri="" prefix="bean" %>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Hello World</title>
<bean:write name="helloWorldForm" property="message"/>

After creating all the files the directory structure of the application looks like this.

On executing the application the "Hello World" message gets displayed to the user.

You can download the source code of this example by clicking on the Download link below.

Source: Download
War: Download

Published at DZone with permission of its author, Meyyappan Muthuraman.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)



Himanshi Bisaria replied on Wed, 2013/10/23 - 10:52am

Hi I am not able to find jar files for Hello World struts example. On downloading the folder the lib folder is empty can you please help?

Kowthalganesh K... replied on Fri, 2013/11/08 - 12:28am in response to: Himanshi Bisaria

note down the jar file details.

visit the site it contains all the jar files search the jar and download the particular jar with exact version and then load the jar file to ur lib folder

Kowthalganesh K... replied on Fri, 2013/11/08 - 12:45am in response to: Himanshi Bisaria

note down the jar file details.
visit the site it contains all the jar files search the jar and download the particular jar with exact version and then load the jar file to ur lib folder

Shiva Kumar replied on Fri, 2014/01/17 - 5:37am

Hi .

i got an error in <b>struts-config.xml</b>file 

 when i am  usig  the same code  in eclipse .

i got an error below like this .

Referenced file contains errors ( For more information, right click on the message in the Problems View and select "Show Details..."

please tell me how to solve this problem 

thank  advance

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.