struts2中常用Result类型type的用法

来源:互联网 发布:西安交大网络教育官网 编辑:程序博客网 时间:2024/05/21 19:48
struts 2中常用result类型(type)的用法关键字: struts 2一、dispatcher(1)为缺省的result类型,一般情况下我们在struts .xml会这么写:<result name="success">/main.js p</result>以上写法使用了两个默认,其完整的写法为:    <result name="success" type="dispatcher">               <param name="location">/maini.js p</param>      </result> 第一个默认:type="dispatcher";第二个默认:设置的为location参数,location只能是页面,不能是另一个action(可用type="chain"解决)。(2)实现方式从doexecute开发方法 看出,有三个出口(finallocation为要跳转的地址):pagecontext.include(finallocation);dispatcher.forward(request, response); (dispatcher是根据finallocation创建的)dispatcher.include(request, response);而我们知道,forward与include都是转发到context内部的资源。二、redirect(1)可以重定向到一个页面,另一个action或一个网址。    <result name="success" type="redirect">aaa.js p</result>      <result name="success" type="redirect">bbb.action</result>      <result name="success" type="redirect">www.baidu.com</result>  (2)实现方式:查看doexecute开发方法 ,只有一个出口:response.sendredirect(finallocation);sendredirect是重定向,是重新产生一个http请求到服务器开发,故重定向后其原来所在的action上下文就不可用了。三、chain(1)主要用于把相关的几个action连接起来,共同完成一个功能。    <action name="step1" class="test.step1action">               <result name="success" type="chain">step2.action</result>      </action>            <action name="step2" class="test.step2action">               <result name="success">finish.js p</result>      </action>(2)实现方式:查看execute()开发方法 ,主要思想如下:// 根据action名称finalactionname及要调用的开发方法 finalmethodname来new一个代理对象proxy,并执行之    proxy = actionproxyfactory.createactionproxy(finalnamespace,                          finalactionname, finalmethodname, extracontext);       proxy.execute();(3)多个action间数据的传递主要有两种方式:1。由于处于chain中的action属于同一个http请求,共享一个actioncontext,故可以在上下文中获取,在页面上可以直接使用。手动获取的开发方法 如下:    httpservlet开发 request request = servlet开发 actioncontext.getrequest();       string s=(string)request.getattribute("propname");  2。实现modeldriven接口在step1action中,加入getmodel:    public object getmodel() {                      return message;       } 在step2action中,加入setmodel:    public void setmodel(object o){                system.out.println("message is:"+o);       }注意,setmodel的调用先于execute()开发方法 后于构造开发方法 。


0 0
原创粉丝点击