性能提升10倍的Ajax远程脚本调用引擎
Posted 大峡
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了性能提升10倍的Ajax远程脚本调用引擎相关的知识,希望对你有一定的参考价值。
EasyJWeb中已经有了一个Ajax远程脚本调用引擎,这是很早的事情了。但由于一直存在一些性能问题,所以一直没有给大家推荐,直到最近m3发布,通过对Ajax远程脚本调用引擎进行完善,把处理复杂对象的返回进行的简化,通过在以前的几个应用中进行了测试,发现性能一下子提升了至少10倍以上,并且运行比较稳定,因此在这里给大家推荐推荐。
在线示例:http://easyjweb.demo.easyjf.com/ajax/ajax.html
完整的示例应用及源码下载:ftp://ftp1.easyjf.com/easyjweb/demo/ajax.war
EasyJWeb是一个提供了对Ajax远程脚本调用功能支持的框架,他帮助你完成Ajax远程脚本调用中的大多数细节工作,你基本上不用写额外的代码就能非常轻松的使用javascript来调用服务器的业务组件的相关方法,这些业务组件可以是任何java对象,比如EJB、EasyJWeb窗口中的Bean、Spring容器中的Bean等等。使用EasyJWeb的Ajax远程脚本调用,需要下面的几个步骤:
1、 在EasyJWeb容器或子容器中配置供客户端脚本调用的业务组件。比如,在easyjweb的配置文件中像下面的方式配置Bean:
![](https://image.cha138.com/20210515/c38550ff48d541618f72af9e8e038f6c.jpg)
PersonServiceImpl.java代码如下:
![](https://image.cha138.com/20210515/c38550ff48d541618f72af9e8e038f6c.jpg)
![](https://image.cha138.com/20210515/c38550ff48d541618f72af9e8e038f6c.jpg)
![](https://image.cha138.com/20210515/c38550ff48d541618f72af9e8e038f6c.jpg)
![](https://image.cha138.com/20210515/c38550ff48d541618f72af9e8e038f6c.jpg)
![](https://image.cha138.com/20210515/c38550ff48d541618f72af9e8e038f6c.jpg)
![](https://image.cha138.com/20210515/c38550ff48d541618f72af9e8e038f6c.jpg)
![](https://image.cha138.com/20210515/3345984cf96a4d26b7d3bde2e200b7a4.jpg)
![](https://image.cha138.com/20210515/0f2938251bb749e99e8b88778c93a908.jpg)
![](https://image.cha138.com/20210515/63afc7f463a14cf79b74174e714e458e.jpg)
![](https://image.cha138.com/20210515/1c67509085ad418684fa36d69b1fe442.jpg)
![](https://image.cha138.com/20210515/77376135f4bf4cbf97ec75196c769532.jpg)
![](https://image.cha138.com/20210515/77376135f4bf4cbf97ec75196c769532.jpg)
![](https://image.cha138.com/20210515/35b1a98457124d14a2a4cce5cef04447.jpg)
![](https://image.cha138.com/20210515/63afc7f463a14cf79b74174e714e458e.jpg)
![](https://image.cha138.com/20210515/1c67509085ad418684fa36d69b1fe442.jpg)
![](https://image.cha138.com/20210515/77376135f4bf4cbf97ec75196c769532.jpg)
![](https://image.cha138.com/20210515/cb1c309e2df341399b9518f15b338e2c.jpg)
![](https://image.cha138.com/20210515/c38550ff48d541618f72af9e8e038f6c.jpg)
![](https://image.cha138.com/20210515/c38550ff48d541618f72af9e8e038f6c.jpg)
2、 在EasyJWeb的配置文件,配置要把容器中的哪些业务组件及具体方法暴露给客户端进行远程脚本调用。内容大致如下:
![](https://image.cha138.com/20210515/c38550ff48d541618f72af9e8e038f6c.jpg)
![](https://image.cha138.com/20210515/c38550ff48d541618f72af9e8e038f6c.jpg)
![](https://image.cha138.com/20210515/c38550ff48d541618f72af9e8e038f6c.jpg)
![](https://image.cha138.com/20210515/c38550ff48d541618f72af9e8e038f6c.jpg)
![](https://image.cha138.com/20210515/c38550ff48d541618f72af9e8e038f6c.jpg)
上面的ajax配置信息表示暴PersonService的所有public方法供客户端使用javascript调用。
只需要上面的两步,就完成了服务器端的相关工作。服务器端不再需要写任何Action,EasyJWeb会自动处理客户端发送来的Ajax远程脚本调用,下面我们进一步看看客户端的程序。
3、为了能够在客户端页面中直接使用javascript进行对PersonService业务组件的Ajax远程脚本调用,我们需要在页面中引入下面三个js。
![](https://image.cha138.com/20210515/c38550ff48d541618f72af9e8e038f6c.jpg)
![](https://image.cha138.com/20210515/c38550ff48d541618f72af9e8e038f6c.jpg)
![](https://image.cha138.com/20210515/c38550ff48d541618f72af9e8e038f6c.jpg)
![](https://image.cha138.com/20210515/c38550ff48d541618f72af9e8e038f6c.jpg)
其中第一个ejf/easyajax/prototype.js引入的是prototype.js,这是一个javascript的基础库,提供很多对基础javascript类的扩展,并提供了ajax相关的实用组件,EasyJWeb的远程脚本调用引擎是基于prototype.js构建的。
第二个ejf/easyajax/engine.js是EasyJWeb的远程脚本调用支持引擎,他负责处理Ajax远程脚本调用的相关的细节,如参数处理、回调处理等,同时提供了一些实用工具如EasyAjaxUtil等供客户端使用。
第三个ejf/easyajax/PersonService.js是PersonService对象,PersonService.js的内容是根据服务器端的容器中名为PersonService的Bean及EasyJWeb的ajax配置信息生成的。只要引入了PersonService.js,我们就可以直接在页面中调用PersonService的相关方法了。
上面的三个js文件都是服务器端动态生成的,需要我们在web.xml文件把所有/ejf/*的URL都交由EasyJWeb来处理。因此,需要在Web.xml的url中增加如下的映射内容:
![](https://image.cha138.com/20210515/c38550ff48d541618f72af9e8e038f6c.jpg)
![](https://image.cha138.com/20210515/c38550ff48d541618f72af9e8e038f6c.jpg)
![](https://image.cha138.com/20210515/c38550ff48d541618f72af9e8e038f6c.jpg)
![](https://image.cha138.com/20210515/c38550ff48d541618f72af9e8e038f6c.jpg)
4、 在页面中使用javascript调用服务器的相关方法
![](https://image.cha138.com/20210515/3345984cf96a4d26b7d3bde2e200b7a4.jpg)
![](https://image.cha138.com/20210515/0f2938251bb749e99e8b88778c93a908.jpg)
![](https://image.cha138.com/20210515/63afc7f463a14cf79b74174e714e458e.jpg)
![](https://image.cha138.com/20210515/1c67509085ad418684fa36d69b1fe442.jpg)
![](https://image.cha138.com/20210515/77376135f4bf4cbf97ec75196c769532.jpg)
![](https://image.cha138.com/20210515/63afc7f463a14cf79b74174e714e458e.jpg)
![](https://image.cha138.com/20210515/1c67509085ad418684fa36d69b1fe442.jpg)
![](https://image.cha138.com/20210515/77376135f4bf4cbf97ec75196c769532.jpg)
![](https://image.cha138.com/20210515/35b1a98457124d14a2a4cce5cef04447.jpg)
![](https://image.cha138.com/20210515/cb1c309e2df341399b9518f15b338e2c.jpg)
![](https://image.cha138.com/20210515/c38550ff48d541618f72af9e8e038f6c.jpg)
怎么样,一切都是如此简单,不需要写任何PersonService的代码,不需要写任何Action,只需要进行非常简单的配置,就能让我们服务器上的业务组件支持Ajax远程脚本调用。
以上是关于性能提升10倍的Ajax远程脚本调用引擎的主要内容,如果未能解决你的问题,请参考以下文章