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.
当一个Action类方法完成以后,他返回一个String。这个String值用来选择结果元素。一个Action映射可能有一系列的结果来展示不同的可能输出。有一系列标准的结果令牌被ActionSupport类定义。
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.
Result元素有两个任务,第一,提供了一个逻辑名,一个action能返回类似“success”或“error”,其实并不知道任何其他细节信息。第二,result元素提供了一个Result Type。大多数的result仅仅表明转入一个serverpage或template,但是其他类型的Result Type能被用来做更加有用的事情。
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.
<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 name="success" type="dispatcher">
<param name="location">/ThankYou.jsp</param>
</result>
<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>/ThankYou.jsp</result>
Mixing results with intelligent defaults with other results makes it easier to see the "critical path".
<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.
<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.