web页面模块化异步渲染:struts-gpipe

yn6e 9年前

struts-gpipe 提供了将 groovy 引入 struts java web 项目的功能, web页面模块化异步渲染。


这个项目的初衷是为了将groovy引入我们的struts web项目,在开发的过程中,发现有跟多可以做的是全,不过在最初的版本,struts-gpipe主要包括两个功能:

1,经过简单的配置可以将groovy引入struts项目,这个时候我们可以将一些业务逻辑代码从java code中提取出来放在groovy里面,至于为什么要把一些业务逻辑代码放在groovy里面,这是因为我们可以利用groovy动态语言的特性,想一想,我们发布的时候再也不需要将java code编译成class然后在deploy。或许我们可以一秒钟搞定发布..这一点是振奋人心的..:)。但是这秒发的功能在struts-gpipe 在最初的版本中并没有做,后面会陆续加入。

2,struts-gpipe的第二个主要功能是实现了异步响应的思想,我们通常的 web页面都是将所有内容渲染完才返回到页面的,如果这个页面很笨重,那么用户可能会遭遇到“白屏”的体验,这对产品来说简直是体验大打折扣,struts-gpipe项目提供了将整个页面拆分成若干模块的功能,每一个模块都独立渲染,多个模块并发渲染,我们可以让一部分模块渲染完成之后先返回给客户端,让另一部分模块渲染之后后返回给客户端,不用想,这样用户体验会好很多。

struts-gpipe与struts和spring的整合:

1,struts-gpipe提供注入spring容器bean的功能,用法一致。

2,struts-gpipe提供ongl的功能,也就是会自动将模块属性引入 freemarker渲染,这一点是和spring mvc不同的,用过spring mvc的都知道,spring mvc向freemarker传入数据需要往model传入塞,而这一点struts使用了ongl技术,让用户不用写塞数据的冗余代码。

struts-gpipe开发的时候尽量考虑到了用户的使用成本,因此将struts-gpipe引入项目配置起来相对还是比较简单的,下面简单介绍一下如何将struts-gpips引入我们的项目:

1.web.xml配置

    <filter>          <filter-name>struts2</filter-name>          <filter-class>com.gweb.filter.GStrutsPrepareAndExecuteFilter</filter-class>          <init-param>              <param-name>config</param-name>              <param-value>struts-default.xml,struts-gpipe.xml,struts-plugin.xml,struts/struts.xml</param-value>          </init-param>      </filter>

struts-gpipe过滤器继承了StrutsPrepareAndExecuteFilter,在启动的时候会初始化struts-gpipe容器。

2,struts.xml配置

<constant name="gweb.groovy.dir" value="biz" />

   <package name="gweb" namespace="/index" extends="gpipe-default">                  <action name="index" class="com.gweb.front.action.Index">                          <result name="success" type="gweb">/WEB-INF/ftl/index.ftl</result>                  </action>          </package>

第一个配置的struts常量是配置groovy脚本的的路径,这个路径可以相对resource目录,第二个配置是需要使用struts-gpipe的package需要继承gpipe-defaule的配置,第三个配置表示一种struts的返回类型。struts-gpipe为struts返回类型添加了一个类型“gweb”,struts-gpipe会代理处理index.ftl的渲染。

3,脚本示例代码

@GPipeMapping("m1")  @Asyn  class M1 implements GPipe{        private String m1;        @Override      String execute() {          m1 = "build by m1.groovy";          return "/WEB-INF/gpipe/m1.ftl"      }        String getM1() {          return m1      }        void setM1(String m1) {          this.m1 = m1      }  }

这里需要配置模块的名字,渲染方式:同步还是异步,返回的ftl路径。模块的名字和主模块里面名字对应:

sync:  ${GPipe_m1}    anyn:  ${GPipe_m2}

其中GPipe_是我给模块名字起的前缀,这个固定,后面的对应groovy脚本里面用注解配置的名字。

项目主页:http://www.open-open.com/lib/view/home/1431254705138

</span>