前几天在项目中需要用到一些ajax 的东西,之前也有用过dwr ,这次想看看其他也些ajax的技术,有什么不同和特殊之处,于是就看了看 jsonrpc 和buffalo ,用起来,突然发现,他们的使用方法,基本上差不多。这里以一个例子做简单的介绍,希望对一些朋友能有所帮助,同时也是对我之前的两篇关于tapestry以及maven的文章的补充(当然,那一系列的文章我还会继续写的)
这个实例是使用tapstry + spring 以及 maven的(同时提供依赖关系,可以不使用maven运行此实例,关于maven的使用,如果有疑问,也可以在此询问我,^_^),这里会同时提供一个war 包,可以直接放入 tomcat 或者jetty容器中直接运行。
这个例子很简单,客户端发送一个字符串到服务器,然后服务器会返回一个包装后的字符串,并且由客户端处理,显示,这个例子中同时使用 jsonrpc buffalo ajax的技术做演示。实现同时,的效果。
先说明依赖关系,
jsonrpc 依赖 jsonrpc-1.0.jar 文件,以及一个 js文件(jsonrpc.js)
buffalo 依赖buffalo-2.0-alpha4.jar 文件,以及一个 js文件(buffalo.js)
同时这连个 ajax 都依赖 prototype(prototype.js)
配置buffalo:
建立标准的 spring bean 的xml配置文件
xml 代码
- <!---->xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:aop="http://www.springframework.org/schema/aop"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
- http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd ">
-
- <bean name="buffaloConfigBean"
- class="net.buffalo.service.BuffaloServiceConfigurer">
- <property name="services">
- <map>
- <entry key="helloWorld">
- <ref bean="helloWorld" />
- entry>
- map>
- property>
- bean>
- beans>
ref bean="helloWorld" 中的 helloWorld 指的是 spring bean id, entry key 中的 helloworld 指的是,客户端js中需要使用的名称
js 代码
- var END_POINT="bfapp";
- var buffalo = new Buffalo(END_POINT);
-
- function buffaloHello() {
-
-
- var pp = 'lococode!';
-
-
- buffalo.remoteCall("helloWorld.sayHelloWorld",[pp], function(reply) {
- alert("buffalo ajax replay one:" + reply.getResult());
- });
-
-
- buffalo.remoteCall("helloWorld.sayHelloWorld",[pp], helloCallBack);
- }
-
-
- function helloCallBack(reply){
- alert("buffalo ajax replay two:" + reply.getResult());
- }
remoteCall 方法中的第一个参数中的helloWord 就是刚刚配置中的 entry key
同时需要 web.xml中配置一个sevlet
xml 代码
-
- <servlet>
- <servlet-name>bfappservlet-name>
- <servlet-class>
- net.buffalo.web.servlet.ApplicationServlet
- servlet-class>
- servlet>
- <servlet-mapping>
- <servlet-name>bfappservlet-name>
- <url-pattern>/bfapp/*url-pattern>
- servlet-mapping>
对于jsonrpc 配置基本上和 buffalo 相同,但是由于 jsonrpc 本身没有提供 和spring的配置集成,所以这个示例提供一个集成代码:
故配置文件如下:
jsonrpc.xml
xml 代码
- <!---->xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:aop="http://www.springframework.org/schema/aop"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
- http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd ">
-
- <bean name="jsonrpcServiceConfigurer"
- class="org.jsonrpc.service.spring.JsonrpcServiceConfigurer">
- <property name="services">
- <map>
- <entry key="helloWorld">
- <ref bean="helloWorld" />
- entry>
-
-
-
- map>
- property>
- bean>
- beans>
-
js 代码
- jsonrpc = new JSONRpcClient("/JSON-RPC");
-
- function jsonrpcHello()
- {
-
- var pp = "lococode!";
-
- var result = jsonrpc.helloWorld.sayHelloWorld(pp);
- alert("jsonrpc ajax replay:" + result);
- }
js调用方法有所区别。
web.xml的配置多了一个listener
xml 代码
-
- <listener>
- <listener-class>
- org.jsonrpc.service.RegistServiceToJsonrpcBridgeListener
- listener-class>
- listener>
- <servlet>
- <servlet-name>
- com.metaparadigm.jsonrpc.JSONRPCServlet
- servlet-name>
- <servlet-class>
- com.metaparadigm.jsonrpc.JSONRPCServlet
- servlet-class>
- servlet>
- <servlet-mapping>
- <servlet-name>
- com.metaparadigm.jsonrpc.JSONRPCServlet
- servlet-name>
- <url-pattern>/JSON-RPCurl-pattern>
- servlet-mapping>
同时需要注意,在 applicationContext.xml 需要导入 jsonrpc.xml 和buffalo.xml
xml 代码
- <!---->xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:aop="http://www.springframework.org/schema/aop"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
- http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd ">
-
-
- <import resource="buffalo.xml" />
-
-
- <import resource="jsonrpc.xml" />
-
- <bean id="musicLibrary"
- class="org.example.myapp.musiclib.services.MusicLibraryImpl"
- scope="session">
- bean>
-
-
- <bean id="helloWorld"
- class="org.example.myapp.musiclib.spring.HelloWorldImpl">
- bean>
-
- beans> jsonrpcUtil.js
-
至此,两个ajax就可以同时跑起来了。当然一个项目中用一种技术就够了,这里只是做一个例子参考。
jsonrpc 会在速度上会具有一些优势。