Buffalo Ajax Instance

Source: Internet
Author: User

Buffalo is the AJAX framework developed by our countrymen

It enables users to invoke methods in Java code in JS.


Configuration method:

1. The relevant servlet is configured in Web.xml as follows:


<?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>
<servlet-name>buffalo</servlet-name>
<url-pattern>/buffalo/*</url-pattern>
</servlet-mapping>


</web-app>

2. The jar packages that need to be introduced are:

Add two jar Packs: Buffalo-2.0.jar and Commons-logging.jar. Note: If Commons-logging.jar does not join, an exception will be thrown.

3. Write the business class that needs to be invoked.
Such as:


1. Helloservice.java


As follows: Three are defined in the method. Returns the string, returns the value object Vo, returns an array of objects,

Package Com.artron.ajax.demo;

Import Com.artron.art.vo.Art;

Public class HelloService {
 
 public string SayHello (string name)
 {       
  return "Hello," + name + ", Welcome to use 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;
}

}


Related Value objects:

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. Configure the business class in the configuration file

Configuration file is: Buffalo-service.properties location under Classes

The contents are as follows:


HelloService = Com.artron.ajax.demo.HelloService

Multiple classes are configured with multiple: format name = Business Class Full Name

Note: JS in the HelloService to replace the Com.artron.ajax.demo.HelloService class to execute the business method


End to this background code

The following is called by the foreground

5. Introduction of JS file in JSP: Prototype.js and Buffalo.js files

<script type= "Text/javascript" src= "Js/prototype.js" ></script>
<script type= "Text/javascript" src= "Js/buffalo.js" ></script>

6. Write Call JS code

Note: HelloService This is the name configured in the configuration file

<script type= "Text/javascript" >
var endPoint = "<%=request.getcontextpath ()%>/buffalo";
var buffalo = new Buffalo (endPoint);
function SayHello (name)
{
The first parameter is the method that invokes the business, the second is the argument list, enclosed in [], and the third is the callback interface.
All that needs to be called can be written in this function.

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);
}

Return JS Object
function Getart (ID) {
var buffalo = new Buffalo (EndPoint, false);
var ret = null;
Buffalo.remotecall ("Helloservice.getart", [id], function (Reply) {
ret = Reply.getresult ();
});
return ret;
}

//Call returns a JS object array
 function getarts (id) {
 var buffalo = new Buffalo (EndPoint, false);
&NBSP;VAR ret = null;
 buffalo.remotecall ("Helloservice.getarts", [id], function (reply) {
  ret = Reply.getresult () ;
 });
 return ret; 
}
 
</script>   


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

<input type= "button" value= "Buffalo Remote Invoke" onclick= "SayHello ($ (' myname '));" />

<div id= "MSG" ></div>


Click the button to show what the Java class method returns


Complete Example:

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" >
<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 are my page" >
<!--
<link rel= "stylesheet" type= "Text/css" href= "Styles.css" >
-->

<body>
This is the My JSP page. <br>

<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 ()%>/buffalo";
var buffalo = new Buffalo (endPoint);
function SayHello (name)
{
The first parameter is the method that invokes the business, the second is the argument list, enclosed in [], and the third is the callback interface.
All that needs to be called can be written in this function.

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);
}


function Getart (ID) {
var buffalo = new Buffalo (EndPoint, false);
var ret = null;
Buffalo.remotecall ("Helloservice.getart", [id], function (Reply) {
ret = Reply.getresult ();
});
return ret;
}

 function getarts (id) {
 var buffalo = new Buffalo (EndPoint, false);
&NBSP;VAR ret = null;
 buffalo.remotecall ("Helloservice.getarts", [id], function (reply) {
  ret = Reply.getresult () ;
 });
 return ret; 

 
    </script>   
     <input type= "text" value= "id= myname"/>
   
    <input Type= "button" value= "Buffalo Remote Call" onclick= "SayHello ($ (' myname '));" />
   
    <div id= "msg" ></DIV>
   
  </body>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.