Struts 2 Annotation Example

Struts 2 Annotation Example explains step by step example of configuring Struts 2 annotation with eclipse

Apache Struts 2, is a popular Java Model-View-Contraller (MVC) framework, which is developed by merging WebWork and Struts 1.x web frameworks.

Struts 2 have lot off difference with struts 1, struts 2 have implemented with an idea of convention over configuration, it also have many annotations in order to increase the developer productivity.

Features like interceptors, OGNL expression & value stack model are some of the benefits, you will get when you are developing with struts 2

Here we are using annotations inside Action class, so that we can remove struts.xml configuration file


When you deploy your struts 2 web application, you may get FilterDispatcher is deprecated! Please use the new filters! warning message, this is because FilterDispatcher filter is deprecated since Struts version 2.1.3, you need to re-configure your application with StrutsPrepareAndExecuteFilter


By default Struts2 Convention plug-in tries to scan, action classes inside the packages like strut, struts2, action or actions. In our example package name is like, so action class will taken from this package.

Also you need to implement com.opensymphony.xwork2.ActionSupport for annotations support

Required Libraries
  1. JDK 6
  2. Eclipse 3.7
  3. struts-

Following jar must be in classpath

  1. asm-3.3.jar
  2. asm-commons-3.3.jar
  3. commons-fileupload-1.3.jar
  4. commons-io-2.0.1.jar
  5. commons-lang3-3.1.jar
  6. commons-logging-1.1.3.jar
  7. freemarker-2.3.19.jar
  8. javassist-3.11.0.GA.jar
  9. ognl-3.0.6.jar
  10. struts2-convention-plugin-
  11. struts2-core-
  12. xwork-core-

You can see the project structure below

Struts 2 Example


<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib uri="/struts-tags" prefix="s"%>
	<s:form action="HelloUser">
		<s:textfield name="Name" label="Name" />
		<s:submit />


<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib uri="/struts-tags" prefix="s"%>
		<s:property value="message" />

Here is the action class, logic is implemented here. You can see below Action tag

@Action(value = "HelloUser", results = @Result(name = "result", location = "/success.jsp") })

Here @Action tag is used for mapping the request (ie; result) with correponding response (ie; success.jsp).

package net.javatips.action;

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

import com.opensymphony.xwork2.ActionSupport;

@ResultPath(value = "/")
public class HelloUser extends ActionSupport {

private static final long serialVersionUID = 1L;
private String message;
private String name;

@Action(value = "HelloUser", results = { @Result(name = "result", location = "/success.jsp") })
public String execute() {
setMessage("Hello " + getName());
return "result";

public String getMessage() {
return message;

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

public String getName() {
return name;

public void setName(String name) { = name;



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

Struts 2 Example

Struts 2 Example

1 Responses to "Struts 2 Annotation Example"
  1. soreka 2013-07-25 10:45:59.0

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