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 |
---|
title | 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.
Returning ActionSupport.NONE
(or null
) from an 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.
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 Typeresult 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 packageEach package may set a default result type to be used if none is specified in a result element. If one package extends another, the "child" package can set its own default result, or inherit one from the parent.
Code Block |
---|
title | Setting a default Result Type |
---|
|
<result-types>
<result-type name="dispatcher" default="true"
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
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 |
---|
title | Result element without defaults |
---|
|
<result name="success" type="dispatcher">
<param name="location">/ThankYou.jsp</param>
</result>
|
Code Block |
---|
| xml |
---|
| xml |
---|
title | A Result element using some defaults |
---|
|
<result>
<param name="location">/ThankYou.jsp</param>
</result>
|
...
Code Block |
---|
| xml |
---|
| xml |
---|
title | Result element using more defaults |
---|
|
<result>/ThankYou.jsp</result>
|
...
Code Block |
---|
| xml |
---|
| xml |
---|
title | 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>
|
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 |
---|
|
<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>
|
The name="*" is not a wildcard pattern, it is a special name that is only selected if an exact match is not found.
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 |
---|
|
<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 |
---|
title | Defining global results |
---|
|
<global-results>
<result name="error">/Error.jsp</result>
<result name="invalid.token">/Error.jsp</result>
<result name="login" type="redirect-actionredirectAction">Logon!input</result>
</global-results>
|
...
Code Block |
---|
| java |
---|
| java |
---|
title | FragmentAction implementation fragment |
---|
|
private String nextAction;
public String getNextAction() {
return nextAction;
}
|
...
Code Block |
---|
| xml |
---|
| xml |
---|
title | FragmentAction configuration fragment |
---|
|
<action name="fragment" class="FragmentAction">
<result name="next" type="redirect-actionredirectAction">${nextAction}</result>
</action>
|
If a FragmentAction
method returns "next" the actual value of that result will be whatever is in FragmentAction
's nextAction
property. So nextAction
may be computed based on whatever state information necessary then passed at runtime to "next"'s redirect-action
redirectAction
.
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 |
---|
|
public Result runAction() {
ServletDispatcherResult result = new ServletDispatcherResult();
result.setLocation("input-form.jsp");
return result;
} |