Struts 2 Result Path Configuration with Examples

Filed Under: Struts 2

This is the third post in the Struts 2 series. You might want to check out earlier posts too.

  1. Struts Beginners Tutorial and Hello World XML Example
  2. Struts 2 Example with Annotation

In both the earlier posts, we used JSP pages as the result of action, in this post we will see some important points to note with the result location with example.


<package name="user" namespace="/User" extends="struts-default">
	<action name="home">
		<result>results/login.jsp</result>
	</action>
</package>

Above is the snippet from struts.xml file, notice that result element doesn’t start with a forward slash (/). In this case, struts will look for result JSP page at {WEBAPP-ROOT}/{Namespace}/{result} path. So our JSP file should be present at WEBAPP/User/results/ directory.

But if result element starts with forward slash, then struts look for file in the root folder. So if struts.xml file package element is:


<package name="user" namespace="/User" extends="struts-default">
	<action name="home">
		<result>/results/login.jsp</result>
	</action>
</package>

Then struts will look for result page at WEBAPP/results/ directory.

Now take a look at below package declaration.


<package name="user" namespace="/User/" extends="struts-default">
	<action name="home">
		<result>/login.jsp</result>
	</action>
</package>

Since the namespace is ending with “/” struts will look for result at WEBAPP/User/login.jsp even though result element starts with forward slash.

Let’s see how we can configure the result path if we are using annotation based configuration.


@Namespace("/Test")
@Action("test")
@Result(location="results/login.jsp")
public class TestAction extends ActionSupport {

}

In above case, struts will try to locate file at WEB-INF/content/Test/results/login.jsp.

Usually we don’t put JSP or HTML files inside WEB-INF directory, so we can provide @Result location attribute value starting with forward slash to let struts look for files in the project root directory.

If we change above @Result annotation to @Result(location="/results/login.jsp"), then struts will look for file at WEBAPP/results/login.jsp

We can also use @ResultPath annotation to provide the result pages path.


@Namespace("/Test")
@Action("test")
@ResultPath("/jsps")
@Result(location="login.jsp")
public class TestAction extends ActionSupport {

}

Struts will look for login.jsp at WEBAPP/jsps/Test/ directory.

If you want to change result path from WEB-INF/content directory to some other directory globally, you can do it through struts configuration file.


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

<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
	"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<constant name="struts.convention.result.path" value="/"></constant>
</struts>

That’s all for configuring the result pages location in struts 2, we will look into more of struts 2 features in future posts.

Comments

  1. Alok says:

    Dear Pankaj,

    I tried below thing its not working.
    Can you please explain me why it is?

    Package declaration

    /login.jsp

    /welcome.jsp
    /error.jsp

    Also I kept the result files in User directory (WebApp/User/login.jsp ; WebApp/User/welcome.jsp ; WebApp/User/error.jsp).

    I am getting below error on browser.
    HTTP Status 404 – There is no Action mapped for action name home.

    1. Alok says:

      Package declare – /User/ ends with forward slash, and result pages with forward slash too.

  2. ved prakash says:

    /results/login.jsp

    if we do not start with / in namespace it’s search from root . would u like to explain?

    1. Pankaj says:

      If you would not start “namespace” with “/” then u will get error like “There is no Action mapped for action name”.

      <package name=”user” namespace=”User” extends=”struts-default”>
      <action name=”home”>
      <result>results/login.jsp</result>
      </action>
      </package>

      If you will try to access localhost:8080/Struts2XMLHelloWorld/User/home.action then you will get error result as:

      Struts Problem Report

      Struts has detected an unhandled exception:

      Messages:
      There is no Action mapped for action name home.

Leave a Reply

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

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages