第一种:Buffalo AJAX属性配置方式
一、新建一个web project,加入两个jar包:buffalo-2.0.jar和commons-logging.jar。注:若commons-logging.jar不加入,会抛出异常。
二、在项目的webRoot下加入两个js文件:buffalo.js和prototype.js,prototype.js可以到buffalo-demo下复制。
三、修改web.xml,把下面代码加入:
Xml代码
<servlet>
<servlet-name>bfapp</servlet-name>
<servlet-class>net.buffalo.web.servlet.ApplicationServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>bfapp</servlet-name>
<url-pattern>/bfapp/*</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>bfapp</servlet-name>
<servlet-class>net.buffalo.web.servlet.ApplicationServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>bfapp</servlet-name>
<url-pattern>/bfapp/*</url-pattern>
</servlet-mapping>
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
四、新建一个java类,就是我们用来调用的,我这里命名为:HelloService.java。如下:
Java代码
package com.business;
public class HelloService {
public String sayHello(String name) {
return "Hello," + name +",欢迎使用Buffalo!";
}
}
package com.business;
public class HelloService {
public String sayHello(String name) {
return "Hello," + name +",欢迎使用Buffalo!";
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
在源文件夹src下新建一个属性文件,命名为:buffalo-service.properties,打开输入下面:
helloService = com.business.HelloService
这个属性文件就是我们配置业务类的。
五、上面的配置就差不多,下面我们来打开index.jsp页面,在里面加上:
Js代码
<script type="text/javascript" src="js/prototype.js"></script>
<script type="text/javascript" src="js/buffalo.js"></script>
<script type="text/javascript">
var endPoint = "<%=request.getContextPath()%>/bfapp";
var buffalo = new Buffalo(endPoint);
function sayHello(name) {
//第一个参数是调用业务的方法,第二个是参数列表,用[]括起来,第三个是回调接口,
//需要调用的都可以写在这个函数中
buffalo.remoteCall("helloService.sayHello", [name.value], function(reply){
alert(reply.getResult());
});
}
</script>
<script type="text/javascript" src="js/prototype.js"></script>
<script type="text/javascript" src="js/buffalo.js"></script>
<script type="text/javascript">
var endPoint = "<%=request.getContextPath()%>/bfapp";
var buffalo = new Buffalo(endPoint);
function sayHello(name) {
//第一个参数是调用业务的方法,第二个是参数列表,用[]括起来,第三个是回调接口,
//需要调用的都可以写在这个函数中
buffalo.remoteCall("helloService.sayHello", [name.value], function(reply){
alert(reply.getResult());
});
}
</script>
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
而在body标签中加入:
请输入你的名字:
<input type="text" value="" id="myname"/>
<input type="button" value="Buffalo远程调用" onclick="sayHello($('myname'));"/>
- 1.
- 2.
如果在项目中整合了spring,我们可以使用第二种spring配置方式,享受spring的注入:
一、引入spring jar包,并且把上面的说的两个jar包和两个js同样加入。
二、在web.xml中加入spring配置和buffalo的配置,如:
Xml代码
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>bfapp</servlet-name>
<servlet-class>net.buffalo.web.servlet.ApplicationServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>bfapp</servlet-name>
<url-pattern>/bfapp/*</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>bfapp</servlet-name>
<servlet-class>net.buffalo.web.servlet.ApplicationServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>bfapp</servlet-name>
<url-pattern>/bfapp/*</url-pattern>
</servlet-mapping>
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
三、同样书写上面的业务:HelloService.java。这里就不用要那个buffalo-service.properties属性文件了。这里就可以打开applicationContext.xml配置文件,加入下面的代码:
Xml代码
<bean id="helloService" class="com.business.HelloService"></bean>
<!-- 这里是Buffalo的业务配置,需要用到的都可以在这里配置 -->
<bean id="buffaloServiceBean" class="net.buffalo.service.BuffaloServiceConfigurer">
<property name="services">
<map>
<entry key="helloService" value-ref="helloService"></entry>
</map>
</property>
</bean>
<bean id="helloService" class="com.business.HelloService"></bean>
<!-- 这里是Buffalo的业务配置,需要用到的都可以在这里配置 -->
<bean id="buffaloServiceBean" class="net.buffalo.service.BuffaloServiceConfigurer">
<property name="services">
<map>
<entry key="helloService" value-ref="helloService"></entry>
</map>
</property>
</bean>
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
顺便把这个日志文件log4j.properties加到src下,如下:
Properties代码
log4j.rootLogger=INFO,A1
log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%d %5p [%t] (%F:%L) - %m%n
log4j.rootLogger=INFO,A1
log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%d %5p [%t] (%F:%L) - %m%n
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
四、最后一步是在jsp页面中使用,见上面的第五步(略)。
大功告成,这个AJAX框架还是我国大师开发的,用起来估计是最方便、最简单的一个,非常感谢这位大师,Buffalo AJAX,翻译成中文名字就是“牛、水牛”的意思,Buffalo AJAX牛,呵呵。
【编辑推荐】