Buffalo 学习笔记
注: 如果转载 请注明
原文地址: http://blog.csdn.net/jianglike18/archive/2009/04/10/4062630.aspx
下载最新的buffalo发布版本(http://buffalo.sourceforge.net/download.html),并且创建如下目录:
WEB-INF/classes
WEB-INF/lib
Script
将commons-logging.jar, buffalo-version.jar 包拷贝到WEB-INF/lib目录
将prototype.js, buffalo.js 到 script目录。
在WEB-INF目录下有一个 web.xml文件,可以在下面配置成如下的形式:
在WEB-INF/classes下创建一个文件buffalo-service.properties内容如下:
# Example Service
helloService=example.HelloService(这个会在后面定义)
注:如果需要集成到Spring的环境中那么还需要在Spring配置文件中加入如下的配置:
Buffalo是基于prototype.js基础上进行开发的,使用Class定义Buffalo,例如
var Buffalo = Class.create()。定义了初始化方法initialize()。初始化含有如下参数gateway, async, events, options具体的定义:
gateway:定义buffalo服务类的前缀路由,该值与web.xml中定义Buffalo的servlet元素
Async:同步或者异步的标志
Events:可以自己定义相关的事件,显示加载 出错 异常 超时 等相关事件的处理。
Options:该属性主要是扩充buffalo的options属性,2.0版本中定义了潮湿时间的属性{timeout:30000} 。
创建XMLHttpRequest对象与其他的AJAX框架基本一致。
Buffalo将要传递的服务器的参数都转化为了XM字符串,并指定了参数的类型。
根据创建XML字符串的调用顺序介绍相关的方法:
Buffalo.Call.xml 方法:创建xml字符串的入口。
Buffalo.Call.dataTypeOf:判断参数的类型,我在JS的源代码中进行了相关的注释。
Buffalo.Call.getParamXML (type,data):根据参数的类型和值组织XML字符串,改方法根据参数的类型分别会调用以下方法Buffalo.Call.doArrayXML,Buffalo.Call.doStructXML,Buffalo.Call.doBooleanXML,Buffalo.Call.doNullXML,Buffalo.Call.doValueXML等
,代码中我也注释了。
最终的XML字符串为如下形式
举例:有如下调用程序
var arr = new Array("1111","2222");
buffalo.remoteCall("dyDayFluxService.testCallBack",[arr,arr],function(replyType){alert("test buffalo service!")});
该调用最终产生的xml字符串为:
注:如果传递的参数含有相同的对象那么使用0标签进行标示“0”标示元素顺序(即与第几个参数值是一样)。
一下代码定义了处理响应的函数:
if (this.async) {//如果是异步调用就执行如下方法
this.transport.onreadystatechange = this.onStateChange.bind(this);
this.events["onLoading"](true);
} else { //同步调用
this.response();
}
最终调用的都是Buffalo.response()方法。Buffalo.response() 主要的功能简单描述如下:
1、 …..
2、 response : function() {
3、 this.timeoutHandle.stop();//停止超时判断函数的执行
4、 this.events["onLoading"](false);//隐藏加载等待页面
5、 if (this.transport.responseText && this.transport.status == '200') {//判断响应状态
6、 var reply = new Buffalo.Reply(this.transport);//封装了服务器的响应信息
7、 if (reply.isFault()) {//判断响应的是否成功
8、 //如果失败调用失败时间进行处理(显示服务器返回的失败信息)
9、 if (this.events["onException"]) {
10、 this.events["onException"](reply.getResult());
11、 } else {
12、 Buffalo.Default.showException(reply.getResult());
13、 this.currentCallback(reply); //如果成功就回调我们自定义的函数
14、 }
15、 } else {
16、 this.currentCallback(reply);
17、 }
18、 this.events["onFinish"](reply);//处理完成以后调用的函数
19、 this.requesting = false;
20、 this.nextRemoteCall();
21、 } else {
22、 this.events["onError"](this.transport);
23、 this.requesting = false;
24、 }
25、 }
26、 ………..
注:如果你想将本次的调用跟页面的上的某个表单元素进行绑定,那么你可以使用Buffalo的另外一个函数bindReply : function(service, params, bindElemId, options) 具体的用法:
例如页面有如下表单元素及JS代码如下:
var buffalo = new Buffalo("/bfapp");
buffalo.bindReply("dyDayFluxService.testCallBack",[],"buffalo_list",null);
页面元素:
后台类的定义: callBack.ExecuteClass.java
public ArrayList testCallBack()
{
ArrayList arrayList = new ArrayList();
arrayList.add("111");
arrayList.add("22");
arrayList.add("333");
return arrayList;
}
执行完调用后,页面元素 将“11“,“22”,“33”分别添加为“option”
l Buffalo.Fault 类型
作用:显示 后台返回的 错误信息 对于调式比较有用。
类型定义:Buffalo.Fault = Class.create();
主要方法:initialize(), toString ()
l Buffalo.Bind 类型:
作用:将调用的服务的返回值 与 页面的表单元素进行绑定,当调用服务正常回应时调用改方法自动设置表单元素的值。根据不同的类型调用Buffalo.BindFactory的相关方法。
主要方法:主要包含了bind方法。
l Buffalo.BindFactory 类型
作用:为表单元素设置具体的值,设置的表单类型可以是text hidden password checkbox radio TEXTAREA TABLE SELECT DIV SPAN FORM中的任何一种。
主要方法:bindText(),bindRadioOrCheckbox(),bindSelect(),bindTable(),checkTrue()
l Buffalo.Form 类型
作用:主要包含两个作用,一是将form 的表单元素转化为数组并对应到后台的具体Bean。二是将后台服务的返回数据绑定到form
主要方法:formToBean(),bindForm()
举例 页面的元素与后台Bean的绑定:
页面的代码:
<html>
<head><title>SVG Demonstrationtitle>
<script src="js/prototype.js">script>
<script src="js/buffalo.js">script>
<script type="text/javascript">
var buffalo = new Buffalo("/bfapp");
function testFormToBean()
{
var user = Buffalo.Form.formToBean("form1","buffalo.jianglike.test.User",true);
buffalo.remoteCall("dyDayFluxService.testUser" ,[user], function(reply) {
alert( " return user memo: " + reply.getResult().name);
} );
}
script>
head>
<body>
<form id ="form1">
<p>
<input name="button" type="button" onClick="testFormToBean()" value="测试formToBean">
p>
<p>id:<input name="id" type="text" id="id">
name:<input name="name" type="text" id="name">
age: <input name="age" type="text" id="age">
memo: <input name="memo" type="text" id="memo">
p>
form>
body>
html>
后台 数据bean User的定义:
public class User {
private int id;
private String name;
private int age;
private String memo;
public User() {
}
后台方法的定义 :
public User testUser(User user)
{
return user;
}
<待续.........>