Struts 2 Hello World Application using Eclipse

Learn the process of creating Struts 2 Hello World example in Eclipse IDE. This is step by step tutorial of developing Struts 2 Hello World application in Eclipse integrated development environment.

Struts 2 Hello World Application using Eclipse


Creating Hello World application in Struts 2 version 2.3.15.1 using Eclipse

In this example we will learn how to make a Hello World Example in Struts 2 Framework. This is the first step for beginners in the field of developing applications based on Struts 2 Framework. Here we have used Eclipse IDE for creating the dynamic web application and added the Struts 2 libraries and code into the project. We have used Tomcat 7 to run the example program.

This section will help you in writing your first Struts 2 applications using Eclipse IDE. You can follow the step by step guide to develop your first Hello World example in Struts 2, deploy on Tomcat 7 and test on browser.

Steps to Create Struts 2 Hello World application

Step 1: Download the Struts 2.3.15.1 and extract the zip file. See the tutorial Downloading and installing Struts 2 2.3.15.1.

Step 2: You should download and install latest version of Eclipse IDE on your computer. Also configure the Tomcat 7 on Eclipse IDE so that you can run the examples from the Eclipse IDE itself. If you don't know how to configure Tomcat 7 with Eclipse IDE then just check the tutorial Tomcat 7 in Eclipse Helios.

Step 3: Open the Eclipse IDE and create a dynamic web project as shown below:

Name the project "Struts2HelloWorld" as shown below:

Eclipse IDE will create the project and it will display the project in the Project explorer window as shown below:

Step 4: Create a new package "net.roseindia". Now create a new Java file "HelloWorld.java" under the package "net.roseindia" and add the following code:

package net.roseindia;

import com.opensymphony.xwork2.ActionSupport;

public class HelloWorld extends ActionSupport {

  public String execute() throws Exception {
  setMessage(getText(MESSAGE));
  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 "HelloWorld.java" is our Action class which contains a variable message whose value is picked up from a package.properties and package_es.properties files. You will get the files from the source code of this tutorial. You should copy and add the package.properties and package_es.properties files from our download source code.

Step 5: Create index.jsp file in the "WebContent" directory of the project. Add the following code:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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>Insert title here</title>
</head>
<body>
<p>Struts 2 Tutorial</p>
<p><a href="/Struts2HelloWorld/roseindia/HelloWorld.action">Test Struts 2 
      Hello World example!!</a>
</body>
</html>

In the above code we have the link to execute the Hello World example. Link url is /Struts2HelloWorld/roseindia/HelloWorld.action.

Step 6: Create a new folder names "classes" under WEB-INF folder of the application and create a new xml file "struts.xml" and add following code into it:

<?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="/roseindia" extends="struts-default">

<default-action-ref name="index" />

<action name="HelloWorld" class="net.roseindia.HelloWorld">
<result>/example/HelloWorld.jsp</result>
</action>

</package>

</struts>

In the above code we are declaring our action class "net.roseindia.HelloWorld". The action name given is "HelloWorld" and we will call this action from browser by typing http://localhost:8080/Struts2HelloWorld/roseindia/HelloWorld.action in the browser. Struts 2 framework determines the action to be executed through the action name.

<action name="HelloWorld" class="net.roseindia.HelloWorld">
     <result>/example/HelloWorld.jsp</result>
</action>

Once the action is successfully executed the /example/HelloWorld.jsp will be used to create the view.

Step 7: Create a new directory example under WebContent folder of the application and then create a new jsp page "HelloWorld.jsp" and add following code:

<%@ 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><s:text name="HelloWorld.message"/></title>
</head>
<body>
<h2><s:property value="message"/></h2>
<a href="/Struts2HelloWorld/">Back to Index page</a>
</body>
</html>

Here in the above code <s:property value="message"/> tag is used to print the value of message variable in the action class (HelloWorld java).

Step 8: Finally add the required jar files from the struts-2.3.15.1-all.zip zip file. You have to extract the struts-2.3.15.1-all.zip file and copy the following jar files from the lib directory into WebContent/WEB-INF/lib directory of the Eclipse project:

  • asm-3.3.jar
  • asm-commons-3.3.jar
  • asm-tree-3.3.jar
  • commons-fileupload-1.3.jar
  • commons-io-2.0.1.jar
  • commons-lang3-3.1.jar
  • commons-logging-1.1.3.jar
  • freemarker-2.3.19.jar
  • javassist-3.11.0.GA.jar
  • log4j-1.2.17.jar
  • ognl-3.0.6.jar
  • struts2-core-2.3.15.1.jar
  • xwork-core-2.3.15.1.jar

Step 9: We are done with the work and to test the project, right click on the "Struts2HelloWorld" in the project explorer window and then select Run as and then "Run on Server" as shown below:

Select the "Tomcat v7.0 Server at localhost" and then click Finish button. Eclipse will compile and then deploy the application on Tomcat 7. After successful startup of the application Eclipse will open the url http://localhost:8080/Struts2HelloWorld/ in the browser as shown below:

To run the example click on the link "Test Struts 2 Hello World example !!" . Browser should display the message as shown below:

You have successfully developed Hello World application in Struts 2. Download the code of the tutorial and try it your self.

Read more Struts 2 version 2.3.15.1 tutorials and master Struts 2 latest version. 0