Struts 2 – How to Correctly Set Result Path

Here, result path means the location of JSP files or other view files which Struts 2 will resolve after executing the code in Action classes. These result paths are mentioned in “location” of @Result annotation on top of Action classes.

Once the action class finish execution, it will pass the control to view resolvers. View resolvers the try to find the location of view file which needs to be rendered. This resolution can be mentioned in two ways mostly:

1) Add whole relative path to “location” attribute

Now, Here a simple “/” can change the whole meaning so be careful.

A) With “/” character in location attribute

@Namespace("/default")
@Results({
	   @Result(name="success", location="/WEB-INF/jsp/success.jsp"),
	   @Result(name="input", location="/WEB-INF/jsp/index.jsp")
})
public class TestAction extends ActionSupport 
{
	//More code...
} 

Above definition will resolve the location of jsp file as ::

%PROJECT_PATH%/WEB-INF/jsp/success.jsp

B) Without “/” character in location attribute

@Namespace("/default")
@Results({
	   @Result(name="success", location="WEB-INF/jsp/success.jsp"),
	   @Result(name="input", location="WEB-INF/jsp/index.jsp")
})
public class TestAction extends ActionSupport 
{
	//More code...
} 

Above definition will resolve the location of jsp file as ::

%PROJECT_PATH%/WEB-INF/content/WEB-INF/jsp/index.jsp

Reason: The reason behind is that “/” in start is resolved at project root, and rest of the path is resolved at relative to project root. But when you are not using “/” in start, the default root is assumed to be “/WEB-INF/content” folder and rest of the path relative to this. So beware when you next time, specify the location attribute in @Result annotation.

2) Define resource root path as constant “struts.convention.result.path”

You can define the fixed part of resource i.e. JSP files in this constant and then you are just left with specifying the names of jsp files without worrying about its location in project. Another advantage is that you can later on move the view files in some other folder or even rename the folder name without worrying about its impact. You will need to only change the path in this constant.

This constant can be defined in struts.xml as ::

<struts>
	<constant name="struts.convention.result.path" value="/WEB-INF/jsp/" />
</struts>

and in struts.properties file as ::

	struts.convention.result.path=/WEB-INF/jsp/

After specifying this constant, in Action class we need to only specify the JSP file name and that’s it.

@Namespace("/default")
@Results({
	   @Result(name="success", location="success.jsp"),
	   @Result(name="input", location="index.jsp")
})
public class TestAction extends ActionSupport 
{
	//More code...
}
The second approach involving constant "struts.convention.result.path" is recommended approach.

Happy Learning !!

Comments

Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.