我的漫漫程序之旅

专注于JavaWeb开发
随笔 - 39, 文章 - 310, 评论 - 411, 引用 - 0
数据加载中……

Struts2中的Result类型应用(action...etc跳转)

概况

Result类型 是在Action执行完,一个结果返回后决定发生什么事的类。开发者可以自由的根据他们的应用和环境的需要创建自己的Result类型。例如在WebWork2中,Servlet和Velocity结果类型已经被创建用来显示web应用程序的画面。

注意: 所有的webwork内建的Result类型都实现了com.opensymphony.xwork.Result接口. 这个接口是所有action执行结果的通用接口,不管这个结果是用来显示一个网页还是产生一个email,发送一个JMS消息,等.

Result类型配置中定义了一些类,把它们映射为action配置中可以引用的名字. 也就是为这些类创建便于记忆的键-值对.

snippet of webwork-default.xml
...
<result-types>
    
<result-type name="dispatcher" class="com.opensymphony.webwork.dispatcher.ServletDispatcherResult" default="true"/>
    
<result-type name="redirect" class="com.opensymphony.webwork.dispatcher.ServletRedirectResult"/>
    
<result-type name="velocity" class="com.opensymphony.webwork.dispatcher.VelocityResult"/>
    
<result-type name="chain" class="com.opensymphony.xwork.ActionChainResult"/>
    
<result-type name="xslt" class="com.opensymphony.webwork.views.xslt.XSLTResult"/>
    
<result-type name="jasper" class="com.opensymphony.webwork.views.jasperreports.JasperReportsResult"/>
    
<result-type name="freemarker" class="com.opensymphony.webwork.views.freemarker.FreemarkerResult"/>
    
<result-type name="httpheader" class="com.opensymphony.webwork.dispatcher.HttpHeaderResult"/>
    
<result-type name="stream" class="com.opensymphony.webwork.dispatcher.StreamResult"/>
    
<result-type name="plaintext" class="com.opensymphony.webwork.dispatcher.PlainTextResult" />
</result-types>
...
snippet of your xwork.xml

<include file="webwork-default.xml"/>

<package name="myPackage" extends="default">
  
<action name="bar" class="myPackage.barAction">
    
<!-- default result type is "dispatcher" -->
    
<!-- default result name is "success" -->
    
<result>foo.jsp</result>
    
<result name="error">error.jsp</result>
    
</result>
  
</action>
</package>

Result类型

Webwork提供了一些com.opensymphony.xwork.Result接口的实现来使你的action可以容易的用户交互.这些Result类型包括:

Result定义在xwork xml配置文件(xwork.xml)中的action标签里。如果location参数是result标签的唯一的参数,你可以这样简化:


<action name="bar" class="myPackage.barAction">
  
<result name="success" type="dispatcher">
    
<param name="location">foo.jsp</param>
  
</result>
</action>

或者


<action name="bar" class="myPackage.barAction">
  
<result name="success" type="dispatcher">foo.jsp</result>
</action>

如果你扩展了webwork-default.xml, 那么默认的返回类型是"dispatcher". 同样,如果你没有指定result的名字,默认将是"success". 就是说你可以如下简化:


<action name="bar" class="myPackage.barAction">
  
<result>foo.jsp</result>
</action>

注意 : Parse属性允许的location参数作为表达式.例如你可以这样用:
Struts2中从一个Action跳转到另一个action,必须将type="redirect"


<result name="success" type="redirect">/displayCart.action?userId=${userId}</result>

注意 : 你也可以指定全局Result以便在多个action中使用. 当要为很多不同的action添加相同的结果是这样会节省时间. Result标签和全局Result的更多信息,参见Result配置部分
原文



posted on 2007-12-03 23:11 々上善若水々 阅读(30718) 评论(0)  编辑  收藏 所属分类: Struts2


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


网站导航: