Struts 2 common result types

Dispatcher Result(default set)

Includes or forwards to a view (usually a jsp). Behind the scenes Struts will use a RequestDispatcher, where the target servlet/JSP receives the same request/response objects as the original servlet/JSP. Therefore, you can pass data between them using request.setAttribute() - the Struts action is available.

Shorthand configuration:

1
2
<!-- show list -->
<result name="list">/jsp/customer/list.jsp</result>

Normal configuration:

1
2
3
4
<!-- show list -->
<result name="list" type="dispatcher">
<param name="location">/jsp/customer/list.jsp</param>
</result>

Another parameter:

parse - true by default. If set to false, the location param will not be parsed for Ognl expressions.


Redirect Result

The response is told to redirect the browser to the specified location (a new request from the client). The consequence of doing this means that the action (action instance, action errors, field errors, etc) that was just executed is lost and no longer available. This is because actions are built on a single-thread model. The only way to pass data is through the session or with web parameters (url?name=value) which can be OGNL expressions.


Redirect Action Result

This is better than the ServletRedirectResult because it does not require you to encode the URL patterns processed by the ActionMapper in to your struts.xml configuration files. This means you can change your URL patterns at any point and your application will still work. It is strongly recommended that if you are redirecting to another action, you use this result rather than the standard redirect result.

1
2
<!-- delete a customer -->
<result name="delete" type="redirectAction">customer_list</result>


Chain Result

This result invokes an entire other action, complete with it’s own interceptor stack and result.

It is similar to dispatcher result, but just between actions.

In the end, thanks to Apache Struts 2 Documentation Result Types.