當Action類方法完成,會返回一個字符串,這個字符串值用來選擇result元素。一個action可以映射到一組不同描述的結(jié)果。標準的result是在ActionSupport 這個基類中定義的。
String SUCCESS = "success";
String NONE = "none";
String ERROR = "error";
String INPUT = "input";
String LOGIN = "login";
當然,ap可以自定義其他的result標記來match特定的情況。
Result Elements
result有默認值設(shè)定,如果不指定屬性,則使用success和dispatch來執(zhí)行這個result
<action name="Hello">
<result>/hello/Result.jsp</result>
<result name="error">/hello/Error.jsp</result>
<result name="input">/hello/Input.jsp</result>
</action>
如上顯示,默認情況下,如果返回值是success,則執(zhí)行第一個result,其他的情況執(zhí)行下面的匹配的result
Global Results
大部分時候,results都是嵌套在antion元素中。但是很多results會應(yīng)用到不同的action。在一個安全的應(yīng)用中,一個client試圖訪問一個沒有認證的頁面,那么很多action可能都需要去訪問 logon result。
如果action需要共享result, 一組global result可以為每一個package定義。框架首先查詢嵌套在action中的本地result,如果本地result沒有符合的,則在global result中查詢。
<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>
Dynamic Results
A result may not be known until execution time. Consider the implementation of a state-machine-based execution flow; the next state might depend on any combination of form input elements, session attributes, user roles, moon phase, etc. In other words, determining the next action, input page, etc. may not be known at configuration time.
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:
一個action有時候不可能在執(zhí)行前就知道它的action,這種情況就需要通過動態(tài)result來實現(xiàn)。
首先定義一個屬性,來存放這個值,然后在流程定義中通過EL表達式來取得這個屬性值。這樣就可以通過程序動態(tài)指定需要轉(zhuǎn)發(fā)的值。
private String nextAction;

public String getNextAction() {
return nextAction;
}

you might define a result like this:
<action name="fragment" class="FragmentAction">
<result name="next" type="redirect-action">${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.
總結(jié),struts2對于result提供了很多很靈活的設(shè)置方法,用戶可以使用相應(yīng)的方法處理特定的邏輯。對于各種情況,總能找出相應(yīng)的方法來出來。