Saturday, 24 August 2013

Struts 2 HelloWorld Simple Example


In this example we will see how to create a simple helloworld application using Struts 2 framework

Environment Used

Java 1.7

Eclipse Juno

Apache Tomcat 7

Struts 2 Jars

1. Create a dynamic web project in Eclipse

Create a Dynamic web project in Eclipse and configure apache tomcat as Server. Once it is created download Struts 2 jars and place in to WEB-INF/lib folder. Here is the final structure of the project. (Click here to download this example with jars)


2. Update Web.xml file

Add Struts servlet details in to web.xml

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

3. Create an Action class (

package com.vinod.test;
import com.opensymphony.xwork2.ActionSupport;
public class HelloWorldAction extends ActionSupport {
    private String name;
    public String getName() {
        return name;
    public void setName(String name) { = name;
    public String execute() {
        setName("Hello "+name+" Welcome to Struts 2");
            return SUCCESS;

4. Create a JSP page (index.jsp)

<%@ taglib prefix="s" uri="/struts-tags" %>
<s:form action="helloaction">
    <s:textfield name="name" label="Enter Your Name" /><br>    
    <s:submit value="Submit" align="center" />

5. Create Success.jsp

<%@ taglib prefix="s" uri="/struts-tags" %>
    <s:property value="name" />

6. Create Struts.xml file

<?xml version="1.0" encoding="UTF-8"?>
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    <include file="struts-default.xml"/>
    <package name="com" extends="struts-default">
        <action name="helloaction" class="com.pretech.HelloWorldAction">
            <result name="success">/success.jsp</result>

7. Deploy application in tomcat server

Run the application and give your name for input



Download Application

Struts 2 Helloworld Example