天天看點

Buffalo ajax執行個體

buffalo是國人開發的ajax架構

它可以使使用者在js中調用java代碼裡的方法.

配置方法:

1. web.xml中配置相關servlet 如下:

<?xml version="1.0" encoding="utf-8"?>

<web-app version="2.4"

 xmlns="http://java.sun.com/xml/ns/j2ee"

 xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"

 xsi:schemalocation="http://java.sun.com/xml/ns/j2ee

 http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

  <servlet>

  <servlet-name>buffalo</servlet-name>

  <servlet-class>net.buffalo.web.servlet.applicationservlet</servlet-class>

 </servlet>

   <servlet-mapping>

  <url-pattern>/buffalo/*</url-pattern>

 </servlet-mapping>

</web-app>

2. 需要引入的jar包為:

加入兩個jar包:buffalo-2.0.jar和commons-logging.jar。注:若commons-logging.jar不加入,會抛出異常。

3. 編寫需要調用的業務類。

如:

1. helloservice.java

如下:方法中定義了三個。分别傳回字元串,傳回值對象vo,傳回對象數組,

package com.artron.ajax.demo;

import com.artron.art.vo.art;

public class helloservice {

 public string sayhello(string name)

 {      

  return "hello," + name +",歡迎使用buffalo!";   

 }

 public static void main(string[] args)

 {

  helloservice hs=new helloservice();

  string hellostr=hs.sayhello("yanek");

  system.out.println(hellostr);

 public art getart(long id)

  art art=new art();

  art.setid(id);

  art.setname("aaaa");

  art.setdescription("mmdmd");

  return art;

 public art[] getarts(long id)

  art[] arts=new art[2];

  art art1=new art();

  art1.setid(id);

  art1.setname("aaaa");

  art1.setdescription("mmdmd");

  art art2=new art();

  art2.setid(id+1);

  art2.setname("bbbbb");

  art2.setdescription("cccc");

  arts[0]=art1;

  arts[1]=art2;

  return arts;

}

相關的值對象:

art.java

package com.artron.art.vo;

public class art {

 private long id;

 private string name;

 private string description;

 public string getdescription() {

  return description;

 public void setdescription(string description) {

  this.description = description;

 public long getid() {

  return id;

 public void setid(long id) {

  this.id = id;

 public string getname() {

  return name;

 public void setname(string name) {

  this.name = name;

4. 配置檔案中配置業務類

 配置檔案為:buffalo-service.properties  位置在classes下

 内容如下:

 helloservice = com.artron.ajax.demo.helloservice

 多個類則配置多個: 格式 名稱=業務類全名

 注意:js裡則通過  helloservice 來代替com.artron.ajax.demo.helloservice類執行其中的業務方法

到此背景代碼結束

下面為前台調用

5. 在jsp中引入js檔案:prototype.js 和 buffalo.js 檔案

 <script type="text/javascript" src="js/prototype.js"></script>

 <script type="text/javascript" src="js/buffalo.js"></script>

6. 編寫調用js代碼

注意:helloservice這個是配置檔案中配置的名稱

<script type="text/javascript">  

      var endpoint = "<%=request.getcontextpath()%>/buffalo"; 

      var buffalo = new buffalo(endpoint);   

       function sayhello(name)

       { 

           //第一個參數是調用業務的方法,第二個是參數清單,用[]括起來,第三個是回調接口,

           //需要調用的都可以寫在這個函數中   

             buffalo.remotecall("helloservice.sayhello", [name.value], function(reply){ 

                            //alert(reply.getresult());

                            $('msg').innerhtml= reply.getresult();

                        }

             );

             //alert("ccc="+getart(6));

             var art=getart(6);

             alert("id="+art.id);

              alert("id="+art.name);

              alert("id="+art.description);

             var arts=getarts(6);

               alert("id="+arts[1].description+"--"+arts[1].id);

               alert("id="+arts[0].description+"--"+arts[0].id);

       }

 //傳回js對象     

 function getart(id) {

 var buffalo = new buffalo(endpoint, false);

 var ret = null;

 buffalo.remotecall("helloservice.getart",[id], function(reply) {

  ret = reply.getresult();

 });

 return ret; 

//調用傳回js對象數組

 function getarts(id) {

 buffalo.remotecall("helloservice.getarts",[id], function(reply) {

</script>   

    <input type="text" value="" id="myname"/>

    <input type="button" value="buffalo遠端調用" onclick="sayhello($('myname'));"/>

    <div id="msg"></div>

   點按紐則顯示java類方法傳回的内容

    完整例子:

    index.jsp:

    <%@ page language="java" import="java.util.*" pageencoding="iso-8859-1"%>

<%

string path = request.getcontextpath();

string basepath = request.getscheme()+"://"+request.getservername()+":"+request.getserverport()+path+"/";

%>

<!doctype html public "-//w3c//dtd html 4.01 transitional//en">

<html>

  <head>

    <base href="<%=basepath%>">

    <title>my jsp 'index.jsp' starting page</title>

 <meta http-equiv="pragma" content="no-cache">

 <meta http-equiv="cache-control" content="no-cache">

 <meta http-equiv="expires" content="0">   

 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">

 <meta http-equiv="description" content="this is my page">

 <!--

 <link rel="stylesheet" type="text/css" href="styles.css">

 -->

  </head>

  <body>

    this is my jsp page. <br>

   <script type="text/javascript" src="js/buffalo.js"></script>

     <script type="text/javascript">  

         var buffalo = new buffalo(endpoint);   

    </script>   

  </body>

</html>