DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone >

Struts Hello World Example in Eclipse

Meyyappan Muthuraman user avatar by
Meyyappan Muthuraman
·
Jun. 13, 12 · · Tutorial
Like (2)
Save
Tweet
313.20K Views

Join the DZone community and get the full member experience.

Join For Free

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 {

@Override
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"
          "http://struts.apache.org/dtds/struts-config_1_3.dtd">

<struts-config>

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

	<global-forwards>
        <forward name="helloWorld" path="/helloWorld.do"/>
    </global-forwards>

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

</struts-config>

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="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_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>StrutsExample1</display-name>

  <servlet>
    <servlet-name>action</servlet-name>
    <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
    <init-param>
      <param-name>config</param-name>
      <param-value>/WEB-INF/struts-config.xml</param-value>
    </init-param>
    <load-on-startup>2</load-on-startup>
 </servlet>

  <servlet-mapping>
    <servlet-name>action</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>

  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

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

<%@ taglib uri="http://struts.apache.org/tags-logic" 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="http://struts.apache.org/tags-bean" prefix="bean" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Hello World</title>
</head>
<body>
<bean:write name="helloWorldForm" property="message"/>
</body>
</html>

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

Eclipse

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • OPC-UA, MQTT, and Apache Kafka: The Trinity of Data Streaming in IoT
  • 10 Steps to Become an Outstanding Java Developer
  • Portfolio Architecture Examples: Retail Collection
  • Refactoring Java Application: Object-Oriented And Functional Approaches

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo