Buffalo Ajax的两种配置方式

来源:互联网 发布:电脑桌面壁纸软件 编辑:程序博客网 时间:2024/05/20 07:59

第一种:属性配置方式
一、新建一个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代码 复制代码
  1. <servlet>  
  2.      <servlet-name>bfapp</servlet-name>  
  3.      <servlet-class>net.buffalo.web.servlet.ApplicationServlet</servlet-class>  
  4.  </servlet>  
  5.  <servlet-mapping>  
  6.      <servlet-name>bfapp</servlet-name>  
  7.      <url-pattern>/bfapp/*</url-pattern>  
  8.  </servlet-mapping>  

四、新建一个java类,就是我们用来调用的,我这里命名为:HelloService.java。如下:

 

Java代码 复制代码
  1. package com.business;   
  2.   
  3. public class HelloService {   
  4.            
  5.     public String sayHello(String name) {   
  6.         return "Hello," + name +",欢迎使用Buffalo!";   
  7.     }   
  8. }  

在源文件夹src下新建一个属性文件,命名为:buffalo-service.properties,打开输入下面:
helloService = com.business.HelloService

这个属性文件就是我们配置业务类的。

五、上面的配置就差不多,下面我们来打开index.jsp页面,在里面加上:

Js代码 复制代码
  1. <script type="text/javascript" src="js/prototype.js"></script>   
  2.   <script type="text/javascript" src="js/buffalo.js"></script>   
  3.   <script type="text/javascript">   
  4.    var endPoint = "<%=request.getContextPath()%>/bfapp";   
  5.    var buffalo = new Buffalo(endPoint);   
  6.       
  7.    function sayHello(name) {   
  8.     //第一个参数是调用业务的方法,第二个是参数列表,用[]括起来,第三个是回调接口,   
  9.     //需要调用的都可以写在这个函数中   
  10.     buffalo.remoteCall("helloService.sayHello", [name.value], function(reply){   
  11.      alert(reply.getResult());   
  12.     });   
  13.    }   
  14.   </script>  

   
  而在body标签中加入:
  请输入你的名字:<input type="text" value="" id="myname"/>&nbsp;&nbsp;
    <input type="button" value="Buffalo远程调用" onclick="sayHello($('myname'));"/>

如果在项目中整合了spring,我们可以使用第二种spring配置方式,享受spring的注入:
一、引入spring jar包,并且把上面的说的两个jar包和两个js同样加入。
二、在web.xml中加入spring配置和buffalo的配置,如:

Xml代码 复制代码
  1. <context-param>  
  2.     <param-name>contextConfigLocation</param-name>  
  3.     <param-value>classpath:applicationContext.xml</param-value>  
  4.    </context-param>  
  5.       
  6.    <listener>  
  7.     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
  8.    </listener>  
  9.       
  10.   <servlet>  
  11.    <servlet-name>bfapp</servlet-name>  
  12.    <servlet-class>net.buffalo.web.servlet.ApplicationServlet</servlet-class>  
  13.   </servlet>  
  14.   <servlet-mapping>  
  15.    <servlet-name>bfapp</servlet-name>  
  16.    <url-pattern>/bfapp/*</url-pattern>  
  17.   </servlet-mapping>  

 


三、同样书写上面的业务:HelloService.java。这里就不用要那个buffalo-service.properties属性文件了。
  这里就可以打开applicationContext.xml配置文件,加入下面的代码:

Xml代码 复制代码
  1. <bean id="helloService" class="com.business.HelloService"></bean>  
  2.      
  3.   <!-- 这里是Buffalo的业务配置,需要用到的都可以在这里配置 -->  
  4.   <bean id="buffaloServiceBean" class="net.buffalo.service.BuffaloServiceConfigurer">  
  5.    <property name="services">  
  6.     <map>  
  7.      <entry key="helloService" value-ref="helloService"></entry>  
  8.     </map>  
  9.    </property>  
  10.   </bean>  

 
 顺便把这个日志文件log4j.properties加到src下,如下:

Properties代码 复制代码
  1. log4j.rootLogger=INFO,A1   
  2. log4j.appender.A1=org.apache.log4j.ConsoleAppender   
  3. log4j.appender.A1.layout=org.apache.log4j.PatternLayout   
  4. log4j.appender.A1.layout.ConversionPattern=%d %5p [%t] (%F:%L) - %m%n  

 
 四、最后一步是在jsp页面中使用,见上面的第五步(略)。
大功告成,这个Ajax框架还是我国大师开发的,用起来估计是最方便、最简单的一个,非常感谢这位大师,Buffalo,翻译成
中文名字就是“牛、水牛”的意思,Buffalo牛,呵呵。

原创粉丝点击