Struts 2 Hello World Example

This tutorials explains how to create a simple struts 2 hello world example and deploy in your server. We are manually creating the required files and running the struts 2 application.

Struts 2 Components

  • Action: Create Action class to write your business logic and work as controller. Action classes are used for interaction from the user and send the response back. It is the cort part of the struts architecture.
  • Interceptors: Interceptors are part of action classes and it is special version of controller. One example is exception interceptors where it handles if any exception is thrown by your application. Interceptors are used only when it is necessary.
  • Views: Create views to take input from the users and display the results.
  • Configurations files: Write configuration files to load the action classes and mappings in your application. This includes web.xml, struts.xml,

1. Create The Directory Structure

  1. Create Java packages for writing Actions classes
  2. Create JSP or any other view pages under the web content folder
  3. Create web.xml under WEB-INF folder
  4. Create struts.xml file inside the Java classes. It should be maintained in the class path.

Struts 2 Project Structure

Get Struts 2 Libraries from here

2. Create Struts 2 Action



public class Struts2HelloWorldAction {
	private String userName;

	public String getUserName() {
		return userName;

	public void setUserName(String userName) {
		this.userName = userName;
	public String execute(){
		return "success";

3. Create Input JSP


<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
	<h1>Struts 2 Hello World Example</h1>

	<s:form action="Welcome">
		<s:textfield name="userName" label="Username" />
		<s:password name="password" label="Password" />
		<s:submit />


4. Configure struts.xml File

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts
Configuration 2.1//EN" "">
<constant name="struts.devMode" value="true" />
<package name="hello" extends="struts-default">
	<action name="Welcome" class="" >
		<result name="success">Result.jsp</result>

5. Create Result JSP


<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
	<h1>Struts 2 Hello World Example</h1>
		Hello	<s:property value="userName" />


6. Configure web.xml File

<?xml version="1.0" encoding="UTF-8"?>

6. Run the application

If you access the application http://localhost:8080/Struts2App/Login.jsp. You would see the following output in your screen.

Struts 2 Hello World Example Input Fields

Struts 2 Hello World Example Result

Download Source Code

Leave a Reply

Your email address will not be published. Required fields are marked *

Pin It on Pinterest

Share This

Share this post with your friends!