Action配置中的Result(局部result)

作为< action>的子元素出现的< result>元素,这被称为局部Result
例如:

<action name="helloworldAction" class="cn.javass.action.action.HelloWorldAction">  
            <result name="toWelcome">/s2impl/welcome.jsp</result>  
            <result name="input">/s2impl/login.jsp</result>  
</action> 

Result?——Result是Action执行完后返回的一个字符串(而Action执行完后返回的字符串,就是上面配置的< result>的name属性的值。),它指示了Action执行完成后,下一个页面在哪里。

在Struts2中,预定义了一些Result的字符串常量,如下:
success,显示结果视图给用户
none,不需要显示视图给用户
error,显示错误页面给用户
input,执行Action需要更多的输入信息,回到input对应的页面
login,返回该登陆视图
(注意:如果不配置name属性的话,默认success)

当然,你可以不使用这些字符串常量,而是使用自己定义的字符串,这样做是没有问题的,只要你在Action里面返回的字符串,跟在struts.xml里面配置的result的name属性值一样就可以了。


Result里面用于指定jsp位置的字符串都是固定的。如果我们希望这个字符串是活动的,可以根据某些参数值来变化.
例如:在Action中定义个folder字符串,并在execute中对它赋值:

public class HelloWorldAction extends ActionSupport {  
    private String account;  
    private String password;  
    private String submitFlag;  
    private String folder;  
    public void setFolder(String folder){  
        this.folder = folder;  
    }  
    public String getFolder(){  
        return folder;  
    }  


    public String execute() throws Exception {  
        this.folder = "s2impl";  
        return "toWelcome";  
    }  
    //属性对应的getter/setter方法,省略了  
}  

那么,在< result>的定义中就可以引用folder这个变量,示例如下:

<result name="toWelcome">/${folder}/welcome.jsp</result>  

“${folder}”的写法跟以前在jsp上写的el表达式类似,而里面的“folder”是跟Action的属性相对应的。

ResultType()相关配置
Struts2中,预定义了很多ResultType,其实就是定义了很多展示结果的技术。Struts2把内置的< result-type>都放在struts-default包中
struts-default.xml

<result-types>  
<result-type name="chain" class="com.opensymphony.xwork2.ActionChainResult"/>  
<result-type name="dispatcher"  
class="org.apache.struts2.dispatcher.ServletDispatcherResult"  
default="true"/>  
<result-type name="freemarker"   
    class="org.apache.struts2.views.freemarker.FreemarkerResult"/>  
<result-type name="httpheader"   
    class="org.apache.struts2.dispatcher.HttpHeaderResult"/>  
<result-type name="redirect"   
    class="org.apache.struts2.dispatcher.ServletRedirectResult"/>  
<result-type name="redirectAction"   
    class="org.apache.struts2.dispatcher.ServletActionRedirectResult"/>  
<result-type name="stream"  
class="org.apache.struts2.dispatcher.StreamResult"/>  
<result-type name="velocity" class="org.apache.struts2.dispatcher.VelocityResult"/>  
<result-type name="xslt" class="org.apache.struts2.views.xslt.XSLTResult"/>  
<result-type name="plainText"  
class="org.apache.struts2.dispatcher.PlainTextResult" />  
</result-types>  

每一个< result-type>元素都是一种视图技术或跳转方式的封装。其中的name属性是在< result>元素中如何引用这种视图技术或跳转方式,对应着< result>元素的type属性的值。

  • 名称为dispatcher的ResultType
    是对RequestDispatcher的一个再包装。通过RequestDispatcher来进行页面跳转,将会保持是同一个请求对象。这有什么好处呢?由于是同一个对象,那就意味着有同样的数据.
  • 名称为redirect的ResultType
    redirect采取重定向的方式,本次请求和跳转到下一个页面的请求是不同的对象,因此它们的值是不一样的。
  • 名称为chain的ResultType
    chain是一种特殊的视图结果,用来将Action执行完之后链接到另一个Action中继续执行,新的Action使用上一个Action的上下文(ActionContext),数据也会被传递。
  • 名称为freemarker的ResultType

  • 其他ResultType
    1:velocity:用来处理velocity模板。Velocity是一个模板引擎,可以将Velocity模板转化成数据流的形式,直接通过JavaServlet输出。
    2:xslt:用来处理处理XML/XLST模板,将结果转换为xml输出。
    3:httpheader:用来控制特殊HTTP行为
    4:stream:用来向浏览器进行流式输出


  *注意:其实不光可以使用struts2给我们提供的各种Result,我们自己还可以自定义Result(日后会碰到)。*
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值