Versions Compared

Key

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

Update parent page

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

Code Block
java
java
titlePredefined 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.

Code Block
titleSetting a default Result Type
<result-types>
 <result-type name="dispatcher" class="org.apache.struts.action2.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.

Code Block
xml
xml
titleResult element without defaults
<result name="success" type="dispatcher">
    <param name="location">/thank_you.jsp</param>
</result>
Code Block
xml
xml
titleA Result element using some defaults
<result>
    <param name="location">/thank_you.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.

Code Block
xml
xml
titleResult element using more defaults
<result>/thank_you.jsp</result>

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

Code Block
xml
xml
titleMultiple Results
<action name="Hello">
  <result>/pages/Hello/Result.jsp</result>
  <result name="error">/pages/Hello/Error.jsp</result>
  <result name="input">/pages/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.

Code Block
xml
xml
titleDefining global results
<global-results>
  <result name="error">/pages/Error.jsp</result>
  <result name="invalid.token">/pages/Error.jsp</result>
  <result name="login" type="redirect-action">Logon!input</result>
</global-results>

(lightbulb) For more about results, see Result Types.