风人园

弱水三千,只取一瓢,便能解渴;佛法无边,奉行一法,便能得益。
随笔 - 99, 文章 - 181, 评论 - 56, 引用 - 0
数据加载中……

struts2--Result Configuration

 

当Action类方法完成,会返回一个字符串,这个字符串值用来选择result元素。一个action可以映射到一组不同描述的结果。标准的result是在ActionSupport 这个基类中定义的。

String SUCCESS = "success";
String NONE    
= "none";
String ERROR   
= "error";
String INPUT   
= "input";
String LOGIN   
= "login";

当然,ap可以自定义其他的result标记来match特定的情况。

Result Elements
result有默认值设定,如果不指定属性,则使用success和dispatch来执行这个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,则执行第一个result,其他的情况执行下面的匹配的result

Global Results

大部分时候,results都是嵌套在antion元素中。但是很多results会应用到不同的action。在一个安全的应用中,一个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有时候不可能在执行前就知道它的action,这种情况就需要通过动态result来实现。

 首先定义一个属性,来存放这个值,然后在流程定义中通过EL表达式来取得这个属性值。这样就可以通过程序动态指定需要转发的值。

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.

总结,struts2对于result提供了很多很灵活的设置方法,用户可以使用相应的方法处理特定的逻辑。对于各种情况,总能找出相应的方法来出来。

posted on 2007-06-27 11:29 风人园 阅读(2843) 评论(0)  编辑  收藏 所属分类: Struts2


只有注册用户登录后才能发表评论。


网站导航: