Apache Struts 2 Documentation > Home > Guides > Core Developers Guide > Struts Configuration Elements > Result Configuration
Added by casey, last edited by Ted Husted on Sep 05, 2006  (view change)

When an 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.

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

Of course, applications can define other result tokens to match specific cases.

Result Elements

The result element has two jobs. First, it provides a logical name. An Action can pass back a token like "success" or "error" without knowing any other implementation details. Second, the result element provides a Result Type. Most results simply forward to a server page or template, but other Result Types can be used to do more interesting things.

Intelligent Defaults

A default Result Type can be set as part of the configuration for each package. If one package extends another, the "child" package can set its own default result, or inherit one from the parent.

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

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

Likewise if the name attribute is not specified, the framework will give it the name "success".

Using these intelligent defaults, the most often used result types also become the simplest.

Result element without defaults
<result name="success" type="dispatcher">
    <param name="location">/ThankYou.jsp</param>
</result>
A Result element using some defaults
<result>
    <param name="location">/ThankYou.jsp</param>
</result>

The param tag sets a property on the Result object. The most commonly-set property is location, which usually specifies the path to a web resources. The param attribute is another intelligent default.

Result element using more defaults
<result>/ThankYou.jsp</result>

Mixing results with intelligent defaults with other results makes it easier to see the "critical path".

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

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.

If actions need to share results, a set of global results can be defined for each package. The framework will first look for a local result nested in the action. If a local match is not found, then the global results are checked.

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

For more about results, see Result Types.