开发环境:JDK1.5 + Tomcat6.0.24
一 发送对象与接收对象(简单转换示例)
1 后台类(实体类,服务类)
public class User implements Serializable {
private static final long serialVersionUID = 1L;
private String id = null;
private String firstName = null;;
private String middleName = null;;
private String familyName = null;;
private String age = null;
private String sex = null;
private List interests = null;
//相应的get,set方法,空的构造函数
//重写toString()方法,将会在控制台得到详细的类信息
public String toString(){
return org.apache.commons.lang.builder.
ToStringBuilder.reflectionToString(this);
}
}
您可能要问为什么age这个属性要设置为String呢!待会揭晓!
public class UserService{
public User addUser(User user){
System.out.println("用户信息: "+user);
return user;
}
}
buffalo-service.properties属性文件如下设置:
userService=net.buffalo.service.UserService
2 界面元素
你一定会想到界面各相关元素名字和类的属性名字一样吧!
<div>
<br><form action="" name="form1">
姓氏: <input type="Text" name="firstName" id="firstName" ><br>
名字: <input type="Text" name="middleName" id="middleName" ><br>
姓名: <input type="Text" name="familyName" id="familyName" ><br>
年龄: <input type="Text" name="age" id="age" ><br>
性别: <input type="radio" name="sex" value="1" checked>男性
<input type="radio" name="sex" value="0">女性 <br>
爱好:
<input type="checkbox" name="interests" value="阅读" checked>阅读nbsp; <input type="checkbox" name="interests" value="运动">运动
<input type="checkbox" name="interests" value="写作">写作
<input type="checkbox" name="interests" value="游戏">游戏
<input type="checkbox" name="interests" value="漫画">漫画
<input type="checkbox" name="interests" value="音乐">音乐
</form>
</div>
3 JS引用
<script language="JavaScript" src="script/prototype.js"></script>
<scriptlanguage="JavaScript" src="script/buffalo.js"></script>
4 JS函数
<script language="JavaScript">
var endPoint="<%=request.getContextPath()%>/bfapp";
var buffalo = new Buffalo(endPoint);
funcion addUser(){
var userObj = Buffalo.Form.formToBean("form1",
"net.buffalo.vo.User");
buffalo.remoteCall("userService.addUser", [userObj], function(reply){
var user = reply.getResult();
var userInfo = "姓: "+user.firstName;
userInfo += "\n名字: "+user.middleName;
userInfo += "\n全名称: "+user.familyName;
userInfo += "\n年龄: "+user.age;
userInfo += "\n性别 : "+user.sex;
userInfo += "\n年龄: "+user.age;
userInfo += "\n 爱好: "+user.interests;
$("form_infomsg").innerHTML=userInfo;
});
}
//第二种用法:
var domainObject = "yourdomain.app.Domain";
var object=Buffalo.Form.formToBean("form2", domainObject, true);
var object = Buffalo.Form.formToBean("form1", null, false);
assertEqual('JoshSmith', object.username);
assertEqual('1', object.gendor);
assertEqual('A', object.interest[0]);
assertEqual('C', object.interest[1]);
assertEqual('2', object.option[0]);
assertEqual('3', object.option[1]);
assertEqual('submit', object.submit);
var object = Buffalo.Form.formToBean("form1", null, true);
assertEqual(null, object.submit);
//绑定html元素值
var data = {
username: "Michael",
password: "newpass",
gendor: "boy",
interest: ["B","C"],
option1: ["1","3"],
option2: "3"
}
Buffalo.Form.bindForm("form3", data);
var obj = Buffalo.Form.formToBean("form3");
assertEqual("Michael", obj.username);
assertEqual("newpass", obj.password);
assertEqual("boy", obj.gendor);
assertEqual("B,C", obj.interest);
assertEqual("1,3", obj.option1);
assertEqual("3", obj.option2);
</script>
5 注意事项
Buffalo对于form转换为Bean提供了二种方式,这是普通的应用方式,回答最开始提的问题:Buffalo只对表单中的值作字符串及字符串集合的转换.如你的Bean设置为 int age,它是不会转换的,你就等着看报异常吧.
这个Buffalo是不是提供的这个转换也太不符合实际应用了呀,放心,Buffalo还为你量身定做了第二种强大的转换方式,有了它,你就不用担心了!
二 强大的Map转换机制,选自Buffalo Demo示例.
var a = {};
a[Buffalo.BOCLASS] = "java.util.Map";
a["a"] = "A";
a["b"] = true;
a["c"] = 123.4;
var u = {};
u[Buffalo.BOCLASS] = "net.buffalo.demo.simple.User";
u.id = 234;
u.name = "<xml here>&";
u.age = 17;
u.sex = false;
u.memo = "very beautiful";
a["u"] = u;
如以上的方式,就可以发送任意对象了呀.这样你总满意了吧!任何要求都会满足你了呀
Buffalo之 bind,reply,ajax,view,form应用(二)
http://liuzidong.iteye.com/blog/629898