Struts 2 Hello World Example

 Download this Project

In this article, we will learn how to create a hello world example project using struts2.

1. Development Environment 

  1. Eclipse
  2. JDK 1.8
  3. Tomcat 8.5
  4. Maven

2. Create a Maven Web Project

Below is my dynamic web project struts2-helloworld structure in eclipse. In this example, we are using struts 2.1.8 version.

This project has a single action named “hello-world” and below is the source code for this project.

Code:

package com.simba.demo;

import com.opensymphony.xwork2.ActionSupport;

/**
 * @author Simba
 *
 */
public class HelloWorldAction extends ActionSupport {

  private String name;
  
  public String sayHello() {
      setName("Simba");	
    return SUCCESS;
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }
  
}
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
<%@taglib uri="/struts-tags" prefix="s"%>
<!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>HelloWorld</title>
</head>
<body>
	<h1>
		Hello <s:property value="name" /> !
	</h1>
</body>
</html>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"classpath://struts-2.0.dtd">

<struts>

  <package name="helloWorld" namespace="/" extends="struts-default">
    <action name="hello-world" class="com.simba.demo.HelloWorld" method="sayHello">
      <result>/WEB-INF/jsp/hello-world.jsp</result>
    </action>
  </package>

</struts>
<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>struts2-helloworld</display-name>
  <filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>struts2-helloworld</groupId>
  <artifactId>struts2-helloworld</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  <build>
    <sourceDirectory>src</sourceDirectory>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.7.0</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
      <plugin>
        <artifactId>maven-war-plugin</artifactId>
        <version>3.0.0</version>
        <configuration>
          <warSourceDirectory>WebContent</warSourceDirectory>
        </configuration>
      </plugin>
    </plugins>
  </build>
  <dependencies>
    <dependency>
      <groupId>org.apache.struts</groupId>
      <artifactId>struts2-core</artifactId>
      <version>2.1.8</version>
    </dependency>
  </dependencies>
</project>

3. Testing

In your eclipse configure tomcat  and run your application by selecting Run on server.

When you open the URL: http://localhost:8080/struts2-helloworld/hello-world you will see the below output.

OR

you can also add .action suffix  to your action name to access the output.