初玩Buffalo
页面调用服务器的一个类里面的方法,做下面的步骤就可以了,前提是你配置好了buffalo那个demo。
只需执行下面三个步骤,就可以完成一个简单的乘法调用。
=====================
Spring例子(使用于1.2以前的版本)
=====================
1) HTML页面上
/buffalo/WebContent/pages/simple.html
增加页面输入框
<h4>1 Multipy calculator</h4>
<p>
<input name="double4" type="text" class="input_text" id="double4" size="12">
*
<input name="double5" type="text" class="input_text" id="double5" size="12">
<input type="button" name="Submit" value=" = " onclick="cmdMulitply()">
<input name="double6" type="text" class="input_text" id="double6">
</p>
增加JS调用
function cmdMulitply(){
var double4 =parseFloat(Buffalo.getElementById("double4").value);
var double5 =parseFloat(Buffalo.getElementById("double5").value);
var d6Handle =Buffalo.getElementById("double6");
buffalo.remoteCall("hnisi_service.multiply",[double4,double5], function(reply) {
d6Handle.value = reply.getResult();
alert(reply.getResult());
})
}
2)/buffalo/JavaSource/demo/buffalo-service.properties文件
加入
# 调整所有的Service
hnisi_service=net.buffalo.demo.hnisi.HnisiService
3)/buffalo/JavaSource/demo/net/buffalo/demo/hnisi/HnisiService.java
增加multiply方法
package net.buffalo.demo.hnisi;
public class HnisiService {
public double multiply(double a, double b) {
System.out.println("Calling Multipy...,a="+a+", b="+b+" 结果="+a*b);
try {
Thread.sleep(1000);
} catch (Exception e) {
e.printStackTrace();
}
return a*b;
}
}
=====================
Spring例子
=====================
Buffalo的1.2版本加入了Spring功能,如果你想使用Spring特性,上述的过程如下:
1) HTML页面上
/buffalo/WebContent/pages/simple-spring-2.html
增加页面输入框
<h4>1 Multipy calculator</h4>
<p>
<input name="double4" type="text" class="input_text" id="double4" size="12">
*
<input name="double5" type="text" class="input_text" id="double5" size="12">
<input type="button" name="Submit" value=" = " onclick="cmdMulitply()">
<input name="double6" type="text" class="input_text" id="double6">
</p>
增加JS调用
function cmdMulitply(){
var double4 =parseFloat(Buffalo.getElementById("double4").value);
var double5 =parseFloat(Buffalo.getElementById("double5").value);
var d6Handle =Buffalo.getElementById("double6");
buffalo.remoteCall("hnisi_service.multiply",[double4,double5], function(reply) {
d6Handle.value = reply.getResult();
alert(reply.getResult());
})
}
2)/buffalo/JavaSource/demo/buffalo-service.properties文件
加入
# 调整所有的Service
hnisi_service=net.buffalo.demo.hnisi.HnisiService
3)/buffalo/JavaSource/demo/net/buffalo/demo/hnisi/HnisiService.java
增加multiply方法
package net.buffalo.demo.hnisi;
public class HnisiService {
public double multiply(double a, double b) {
System.out.println("Calling Multipy...,a="+a+", b="+b+" 结果="+a*b);
try {
Thread.sleep(1000);
} catch (Exception e) {
e.printStackTrace();
}
return a*b;
}
}
4)修改/buffalo/WebContent/WEB-INF/applicationContext.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean name="simpleService" class="net.buffalo.demo.simple.SimpleService"></bean>
<bean name="hnisiService" class="net.buffalo.demo.hnisi.HnisiService"></bean>
<bean name="numberService" class="net.buffalo.demo.numberguess.NumberGuessService"></bean>
<bean name="buffaloConfigBean" class="net.buffalo.service.BuffaloServiceConfigurer">
<property name="services">
<map>
<entry key="springSimpleService">
<ref bean="simpleService"/>
</entry>
<entry key="springNumberService">
<ref bean="numberService"/>
</entry>
<entry key="springHnisiService">
<ref bean="hnisiService"/>
</entry>
</map>
</property>
</bean>
</beans>
总体感觉是简洁,无须关注xmlhttp,告别xml让我感到有点欣慰。