Home » Enterprise Java » Struts 2 » Struts 2 @ResultPath Annotation Example

About Andres Jaimes

Avatar photo
Andres graduated from Computer Science in the the Monterrey Institute of Technology and Higher Education. He is currently studying his Masters in Management of Information Technology. During his professional career he has been involved with a large number of Web Java/C++ projects for companies of different industries in the private and public sectors. He enjoys teaching and believes in the spark of innovation we have all received.

Struts 2 @ResultPath Annotation Example

Struts 2, the famous Model-View-Controller (MVC) framework, has several features that allows you to shape your webapp the way you want it.

Among those features, a handy one is @ResultPath, which allows you to change the location where results are stored.

The best way to explain how it works is through an example.

1. Setup a Project

This time we will setup a Maven project.

Go to File > New and create a Dynamic Web Project.


Figure 1: Project Setup


Make sure the Generate web.xml deployment descriptor option is selected.


Figure 2: Project Setup, select the “Generate web.xml” option


Convert the project to a Maven one by right clicking on the project and going to Configure > Convert to Maven.

Set a name for the Maven project:


Figure 3: Assign a name to your new POM


Open your pom.xml file and paste the following dependencies to it:


As you may see in the dependency list we’re adding struts2-convention-plugin too. This is required by Struts to look for action annotations in class files. Don’t forget to add it or Struts will be unable to find and map your actions.

2. Create the project files


Open web.xml and add the following contents:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>Struts 2 Annotation Example</display-name>

Something to note in this file is the use of the init-param parameter. This is used by Struts to know where our action classes are located. It is really important that you create them in a package named actions. Put your classes inside something different than actions and I guarantee you that you will spend the whole afternoon stick to your computer looking for answers.

Let’s create a JSP file now. Create a folder named jsps inside WebContent. Add a new JSP file named mypage.jsp and paste the following content into it:

<%@ taglib prefix="s" uri="/struts-tags" %>
    <h1>Hello Struts 2!</h1>

Piece of cake right? Nothing unusual here, just a corteous salutation.

2. The base Action class

Now it is time to add an action. Create a class named DemoAction and paste the following code into it:


package com.javacodegeeks.actions;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.ResultPath;
import com.opensymphony.xwork2.ActionSupport;

public class DemoAction extends ActionSupport {
    public String execute() {
        return SUCCESS;

Your file structure should look like this:


Figure 4: File and directory structure


Great!, we’re ready to run this application. Go ahead, open a web browser and go to the next URL:


You should see the following page:


Figure 5: Webpage, @ResultPath = /


Let’s take a closer look at the annotations on the previous code.

@Namespace: This annotation helps you structure your webapp. It adds an additional level to your URL and to your directory structure; that’s why mypage.jsp is located at:


Now, since @ResultPath has a value of "/" it instructs Struts to look for your Namespaces / JSP’s starting at the root level; this level is your WebContent folder.

In summary, this is where Struts is looking for your JSP’s:


But let’s change some values to make sure this is clear.

3. Playing with @ResultPath

Now consider the following modification to the previous class:


This will change the place where Struts will look for your JSP’s. Which in this case will be:


To test it, create a folder named user inside WebContent and a folder named jsps inside user. Create a JSP inside jsps and name it mypage.jsp with the following contents:

<%@ taglib prefix="s" uri="/struts-tags" %>
    <h1>Hello Struts 2! (from /user/jsps)</h1>

Your WebContent folder should look like this:


Figure 6: File and directory structure


Run the application and refresh your browser; you will see the following image:


Figure 7: Webpage, @ResultPath = “/user”


4. Global settings

If you want to change the value for all your pages, you can add the next line to your struts.xml file:

    <constant name="struts.convention.result.path" value="/"/>

 5. Download the Eclipse Project

This was a Struts 2 “@ResultPath” example.

You can download the full source code of this example here: ResultPathDemo.zip

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!


1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design


and many more ....


Receive Java & Developer job alerts in your Area

I have read and agree to the terms & conditions


Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Inline Feedbacks
View all comments