buffalo 1 分钟教程

Buffalo AJAX Framework Log In   View a printable version of the current page.
1 分钟教程

Added by Boin, last edited by Michael Chen on Apr 28, 2007  (view change)
Labels: 
(None)


1 分钟教程

准备工作

  • 请先下载最新版本的Buffalo,然后按照以下关系建立文件夹
buffalo-example
WEB-INF/classes
WEB-INF/lib
script
  • 拷贝 commons-logging.jar 和 buffalo-<version>.jar 到WEB-INF下的lib目录,拷贝 prototype.js 和 buffalo.js 到 script 文件夹.
修改 web.xml
  • 在 WEB-INF 目录下建立或修改 web.xml 文件,填写一下内容
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<display-name>Buffalo Example Application</display-name>
<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>
</web-app>
修改 buffalo-service.properties
  • 在 WEB-INF 下 classes 目录中创建一个 buffalo-service.properties 的文本文件,并填写以下内容:
# 示例服务
helloService=example.HelloService
修改 JSP 文件
  • 在 web 根目录创建 example.jsp,并填写以下内容
<html>
<head>
<meta http-equiv="Content-Type" content="text/html">
<title>Example::Hello</title>
<script language="javascript" src="script/prototype.js"></script>
<script language="javascript" src="script/buffalo.js"></script>
<script language="javascript">
var END_POINT="<%=request.getContextPath()%>/bfapp";
var buffalo = new Buffalo(END_POINT);
function hello() {
var p1 = $("myname").value;
buffalo.remoteCall("helloService.hello",[p1], function(reply) {
alert(reply.getResult());
});
}
</script>
</head>
<body>
<p>Buffalo Hello World</p>
<p>&nbsp;</p>
<form name="form1" method="post" action="">
Your name:
<input name="myname" type="text" id="myname">
<input type="button" name="Submit" value="Hello" onclick="hello()">
</form>
</body>
</html>
增加一个服务
package example;
public class HelloService {
public String hello(String name) {
try {
// to see the loading status
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return "Hello, " + name;
}
}
  • 编译并拷贝至 WEB-INF 下的 classes 目录
最后一步: 运行此程序

拷贝 buffalo-example 目录到 TOMCAT_HOME 下的 webapps 中,启动 tomcat,用你喜爱的浏览器浏览 http://localhost:8080/buffalo-example/example.jsp,
在页面中输入你的名称并点击 "Hello" 按钮,查看效果

原文地址:https://www.cnblogs.com/smallfa/p/975422.html