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.
當(dāng)一個(gè)Action類方法完成以后,他返回一個(gè)String。這個(gè)String值用來(lái)選擇結(jié)果元素。一個(gè)Action映射可能有一系列的結(jié)果來(lái)展示不同的可能輸出。有一系列標(biāo)準(zhǔn)的結(jié)果令牌被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元素有兩個(gè)任務(wù),第一,提供了一個(gè)邏輯名,一個(gè)action能返回類似“success”或“error”,其實(shí)并不知道任何其他細(xì)節(jié)信息。第二,result元素提供了一個(gè)Result Type。大多數(shù)的result僅僅表明轉(zhuǎn)入一個(gè)serverpage或template,但是其他類型的Result Type能被用來(lái)做更加有用的事情。
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.