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.
Code Block |
---|
| java | java |
---|
title | Predefined result names |
---|
| java |
---|
|
String SUCCESS = "success";
String NONE = "none";
String ERROR = "error";
String INPUT = "input";
String LOGIN = "login";
|
...
Using these intelligent defaults, the most often used result types also become the simplest.
Code Block |
---|
| xml | xml |
---|
title | Result element without defaults |
---|
| xml |
---|
|
<result name="success" type="dispatcher">
<param name="location">/ThankYou.jsp</param>
</result>
|
Code Block |
xml |
---|
| xml |
---|
title | A Result element using some defaults |
---|
| xml |
---|
|
<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.
Code Block |
---|
| xml | xml |
---|
title | Result element using more defaults |
---|
| xml |
---|
|
<result>/ThankYou.jsp</result>
|
Mixing results with intelligent defaults with other results makes it easier to see the "critical path".
Code Block |
xml |
---|
| xml |
---|
title | Multiple Results |
---|
| xml |
---|
|
<action name="Hello">
<result>/hello/Result.jsp</result>
<result name="error">/hello/Error.jsp</result>
<result name="input">/hello/Input.jsp</result>
</action>
|
A special 'other' result can be configured by adding a result with name="*". This result will only be selected if no result is found with a matching name.
Code Block |
---|
| xml | xml |
---|
title | '*' Other Result |
---|
| xml |
---|
|
<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>
|
...
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 |
---|
title | Defining global results |
---|
| xml |
---|
|
<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>
|
...
Result values may be retrieved from its corresponding Action implementation by using EL expressions that access the Action's properties, just like the Struts 2 tag libraries. So given the following Action fragment:
Code Block |
---|
| java | java |
---|
title | FragmentAction implementation |
---|
| java |
---|
|
private String nextAction;
public String getNextAction() {
return nextAction;
}
|
you might define a result like this:
Code Block |
---|
| xml | xml |
---|
title | FragmentAction configuration |
---|
| xml |
---|
|
<action name="fragment" class="FragmentAction">
<result name="next" type="redirectAction">${nextAction}</result>
</action>
|
...