Struts 2 Result Path Configuration with Examples

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.

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:

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

Now take a look at below package declaration.

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.

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.

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.

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

By admin

Leave a Reply

%d bloggers like this: