最近学习了openamf技术,总结下:
首先需要openamf.war,网上搜索可以得到,此技术主要是用于flash与java交互。
建立java的web项目,建立完成,建立类中的方法,看代码:
package com.cn.flash;
import java.util.ArrayList;
import java.util.List;
public class Hello {
public String getHello(){
return "Hello world!";
}
public List getlist(){
List list = new ArrayList();
Person person = new Person();
person.setId("1");
person.setName("1.name");
person.setSex("1.sex");
Person person1 = new Person();
person1.setId("2");
person1.setName("2.name");
person1.setSex("2.sex");
list.add(person);
list.add(person1);
return list;
}
public Person getPerson(){
Person person = new Person();
person.setId("1");
person.setName("1.name");
person.setSex("1.sex");
return person;
}
public static void main(String arg[]){
Hello h = new Hello();
Person person = new Person();
person = h.getPerson();
// List list=h.getlist();
// person = (Person)list.get(1);
// System.out.println(list.get(0));
System.out.println(person.getName());
}
}
此代码定义了几个方法,其中还有javabean
package com.cn.flash;
public class Person {
private String id;
private String name;
private String sex;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}
很简单,这个也完全适用与spring+hibernate表现层就不用java开发人员关心了。
下面就是通过flash进行远程调用java方法。首先保证刚才的程序,发布,并启动tomcat。
下面看看如何配置,将openamf.war,在tomcat中展开,并将webinf中的内容,copy在您自己的项目中。
最后配置web.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN" "http://java.sun.com/j2ee/dtds/web-app_2.2.dtd"> <web-app> <display-name>flash</display-name> <description>java for flash application</description> <!-- openamf gateways --> <servlet> <servlet-name>DefaultGateway</servlet-name> <display-name>DefaultGateway</display-name> <description>DefaultGateway</description> <servlet-class>org.openamf.DefaultGateway</servlet-class> <init-param> <param-name>OPENAMF_CONFIG</param-name> <param-value>/WEB-INF/openamf-config.xml</param-value> <description> </description> </init-param> </servlet> <servlet-mapping> <servlet-name>DefaultGateway</servlet-name> <url-pattern>/gateway</url-pattern> </servlet-mapping> <session-config> <session-timeout>30</session-timeout> </session-config> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
最后就是flash中的as3代码
package { import flash.display.Sprite; import flash.display.Sprite; import flash.net.NetConnection; import flash.net.Responder; public class Main extends Sprite { private var gateWayUrl:String = "http://localhost:8080/Flash/gateway"; private var remotingNc:NetConnection; public function Main():void { trace("Main.........."); init(); } private function init():void{ remotingNc = new NetConnection(); remotingNc.connect(gateWayUrl); //remotingNc.call("com.cn.flash.Hello.getHello", new Responder(getMsgResult, getMsgFault)); //remotingNc.call("com.cn.flash.Hello.getlist", new Responder(getMsgResult, getMsgFault)); remotingNc.call("com.cn.flash.Hello.getPerson",new Responder(getMsgResult,getMsgFault)); } private function getMsgResult(re:*){ trace("从remoting返回信息成功:", re); //得到对象 var a:Object = new Object(); a = re; trace(a.id); //person.setId(); } private function getMsgFault(fe:*){ trace("从remoting返回信息失败:",fe.code); } } }
此代码,对于as3的学习这应该不难理解,这里就不对说了。
完!