Struts 2 hello world application using annotation
Posted on: January 31, 2011 at 12:00 AM
In this tutorial you will learn about the struts2.2.1 annotation

Struts 2 hello world application using annotation

Annotation is an alternatives of xml in a java application. It is a way to adding metadata facility to the application. for example interfaces and classes defines a type in an application and they can be used to several application elements. The annotation can be a compiler instruction that describes how to compile the code. The annotations are of many types that outline the data it contains. For Example @Action, @Override, @result, @Before, @Test etc. Every annotation type starts with the @ followed be a annotation name or interface name.

For Example to define an action Annotation you may write code as

@Action(value="/actionName",results={@Result(name=".....", location=".....")})

An annotation Based Hello World example is given below which takes user name as input and prints a message Hello plus user name.

package net.roseindia.action;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;

public class HelloWorldAction {
	private static final long serialVersionUID = 1L;
	String userName;
	String message;

	@Action(value = "/hello", results = { @Result(name = "success", location = "/jsp/successPage.jsp") })
	public String execute() throws Exception {
		message = "Hello " + userName;
		return "success";

	public String getMessage() {
		return message;

	public void setMessage(String message) {
		this.message = message;

	public String getUserName() {
		return userName;

	public void setUserName(String userName) {
		this.userName = userName;


<%@ taglib prefix="s" uri="/struts-tags" %>
<title>Annotation Example</title>
<style type="text/css">@import url(css/main.css);</style>
.errorMessage {
<body bgcolor="#A3A3FF">
<h2>Please Enter Your Name</h2>
<s:form action="hello">
<s:textfield name="userName" label="Please Specy Your Name"/> 


<%@ taglib prefix="s" uri="/struts-tags" %>
<title>Annotation Example</title>
<body bgcolor="#9E9EE8">


When you run this application it will display message as shown below:


Download this example code

To See More Example on annotation follow the links given below

Hello World Example1

Hello World Example2

Related Tags for Struts 2 hello world application using annotation :