最近搞了一下Flash高级编程-remoting,网上也有许多类似的教程,发现写的都不太全,完全照搬根本不行,花了一天时间终于搞通了,现整理一下,方便自已也方便他人.
以下代码均通过本人测试通过无误.
因纯属个人笔记,所以省略了许多注释和说明,外行人或半桶子水的人可能看不太懂,就像垃圾,但内行人或需要这方面知识的人可能一看就明白,因为关键的地方都涉及到了,也许对他们有少许帮助.如看不懂的可以提问,或直接和我联系.
注:要看懂本文需要具体一定基础知识,如JAVA/TOMCAT/FLASH
环境:
JDK1.4.2
TOMCAT4.1.25
FLASH 2004 MX
flashremoting_comp_as20-win-en.exe
flashremoting-java-win-en.exe
FlashRemotingComponents-win-en.exe
flashremoting_comp_sourcecode.zip(因为安装时有一个bug,所以需要下载它手动安装remoting)
flashgateway-samples.war
上面的东东可以自已去各官方网下载,都是免费的
http://www.adobe.com/products/flashremoting/downloads/components/
安装顺序:
JDK1.4.2 -> TOMCAT4.1.25
FLASH 2004 MX -> flashremoting_comp_as20-win-en.exe -> flashremoting-java-win-en.exe -> FlashRemotingComponents-win-en.exe
因为remoting本身有一个bug,他的安装程序不会安装rpc下的as文件,所以需要手动安装,方法:把flashremoting_comp_sourcecode.zip解开,将里面的 ” ../mx/rpc/”下的文件拷贝到你的FLASH 2004 MX安装目录下的classes目录下,如:”C:\Program Files\Macromedia\Flash MX 2004\en\First Run\Classes\mx”,注意’rpc’也要拷贝:)
将flashgateway-samples.war文件拷贝到你的tomcat目录的webapps目录下,启动tomcat
到此为止你的环境就构筑好了.
测试:
很简单,写一个.fla和一个.as,二个.java 文件即可(如果要用IE看还需要写一个HTML或JSP)
不费话了,直接以代码视人.
文件1:getUserId.fla
第一贞上写一行代码就行了
include “getUserId.as”
文件2:getUserId.as
import mx.remoting.Service;
import mx.remoting.PendingCall;
import mx.rpc.RelayResponder;
import mx.rpc.FaultEvent;
import mx.rpc.ResultEvent;
var flashtestService:Service = new Service(“http://localhost:8080/flashgateway-samples/gateway“,null, “com.remoting.helloworld.UserJohoBean”, null, null);
var pc:PendingCall = flashtestService.getUserId();
pc.responder = new RelayResponder(this, “getMessage_Result”, “getMessage_Fault”);
response_txt.text =”init string”;
function getMessage_Result(re:ResultEvent) {
trace(“ok!”);
response_txt.text = re.result;
}
function getMessage_Fault(fe:FaultEvent) {
trace(“shit….”);
response_txt.text = “There was a problem”;
}
文件3:UserJohoBean.java
package com.remoting.helloworld;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class UserJohoBean implements Serializable{
public String getUserId(){
try{
DbConnection dc = new DbConnection();
Connection con = dc.getConnection();
System.out.println(“connection = ” + con);
Statement stmt=con.createStatement();
String sql=”select * from name_joho”;
ResultSet rs=stmt.executeQuery(sql);
System.out.println(sql+” execute successfully!!!”);
if(rs.next()) {
// System.out.println(rs.getString(1));
// System.out.println(rs.getString(2));
return rs.getString(1);
} else {
return ” default id”;
}
} catch (SQLException sqle) {
sqle.printStackTrace();
} catch(Exception e) {
e.printStackTrace();
}
return “get failed”;
}
}
文件4:DbConnection.java
package com.remoting.helloworld;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class DbConnection {
private String driver = “oracle.jdbc.pool.OracleConnectionCacheImpl”;
private String url = “jdbc:oracle:thin:@10.1.1.133:1521:jcc”;
private String user = “testtool”;
private String password = “testtool”;
private String maxactive = “5”;
public Connection getConnection() throws SQLException {
Connection con = null;
// ドライバの読み込み
try {
Class.forName(driver).newInstance();
} catch (ClassNotFoundException e) {
throw new SQLException(“driverClass:” + driver + “loaded failed!!!”);
} catch (Exception ee) {
ee.printStackTrace();
}
try {
// コネクション生成
con =
DriverManager.getConnection(url, user, password);
} catch (SQLException sqle) {
throw sqle;
}
con.setAutoCommit(true);
return con;
}
}
文件5:getUserId.html
myfirst_javabean