Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Table of Contents

When an Action action class method completes, it returns a String. The value of the String is used to select a result element. An action mapping will often have a set of results representing different possible outcomes. A standard set of result tokens are defined by the ActionSupport base class.

Code Block
java
java
titlePredefined result names

String SUCCESS = "success";
String NONE    = "none";
String ERROR   = "error";
String INPUT   = "input";
String LOGIN   = "login";

...

(info) Returning ActionSupport.NONE (or null) from an Action action class method causes the results processing to be skipped. This is useful if the action fully handles the result processing such as writing directly to the HttpServletResponse OutputStream.

...

Code Block
titleSetting a default Result Type

<result-types>
   <result-type name="dispatcher" default="true"
                class="org.apache.struts2.dispatcher.ServletDispatcherResult" />
</result-types>

If a type attribute is not specified, the framework will use the default dispatcher type, which forwards to another web resource. If the resource is a JavaServer Page, then the container will render it, using its JSP engine.

...

Code Block
xml
xml
titleResult element without defaults

<result name="success" type="dispatcher">
    <param name="location">/ThankYou.jsp</param>
</result>
Code Block
xml
xml
titleA Result element using some defaults

<result>
    <param name="location">/ThankYou.jsp</param>
</result>

...

Code Block
xml
xml
titleResult element using more defaults

<result>/ThankYou.jsp</result>

...

Code Block
xml
xml
titleMultiple Results

<action name="Hello">
    <result>/hello/Result.jsp</result>
    <result name="error">/hello/Error.jsp</result>
    <result name="input">/hello/Input.jsp</result>
</action>

...

Code Block
xml
xml
title'*' Other Result

<action name="Hello">
    <result>/hello/Result.jsp</result>
    <result name="error">/hello/Error.jsp</result>
    <result name="input">/hello/Input.jsp</result>
    <result name="*">/hello/Other.jsp</result>
</action>

...

(warning) In most cases if an action returns an unrecognized result name this would be a programming error and should be fixed.

Multiple names

It is possible to define multiple names for the same result:

Code Block
languagexml
<action name="save">
    <result>success.jsp</result>
    <result name="error, input">input-form.jsp</result>
</action>

Such functionality was added in Struts 2.5

Global Results

Most often, results are nested with the action element. But some results apply to multiple actions. In a secure application, a client might try to access a page without being authorized, and many actions may need access to a "logon" result.

...

Code Block
xml
xml
titleDefining global results

<global-results>
    <result name="error">/Error.jsp</result>
    <result name="invalid.token">/Error.jsp</result>
    <result name="login" type="redirectAction">Logon!input</result>
</global-results>

...

Code Block
java
java
titleFragmentAction implementation

private String nextAction;

public String getNextAction() {
    return nextAction;
}

...

Code Block
xml
xml
titleFragmentAction configuration

<action name="fragment" class="FragmentAction">
    <result name="next" type="redirectAction">${nextAction}</result>
</action>

...

See Parameters in configuration results for an expanded discussion.

 

Returning Result Objects

Instead of configuring results and returning the name, it is possible to return a result object:

Code Block
languagejava
public Result runAction() {
	ServletDispatcherResult result = new ServletDispatcherResult();
	result.setLocation("input-form.jsp");
	return result;
}