最近看到RIA比较火,也想凑个热闹,把最近几天的学习心得记录下来
给自己看,如果能对新人有所帮助那是最好的,老手就不用看了
开发环境
flash cs3 professional 和flex 3比较
flex 3 比较符合程序员的习惯,尤其是对Java程序员来说,Eclipse 太熟悉了,不过我觉得不能开发多帧的程序是flex 的最大缺点,也可能我孤陋寡闻,没找到开发多帧程序的方式
因为是新手上路,所以弄了几个非常简单的小东西,但弄来弄去还是单帧,不爽
还是选了flash cs3 虽然不符合程序员的习惯,但多帧对我吸引力太大了,因为那样才是真正的应用程序式的开发。
下面主要就flash cs3中开发写一点心得:
1、最开始最大的困难就是不知道脚本和动画如何联系起来,其实很简单,先建个flash (as3)文件,保存为ConnectSrv.fla(名字自己起),再建一个同名的as3文件,保存为ConnectSrv.as ,然后在ConnectSrv.fla 的界面上“属性面板“有个文档类:里面添上ConnectSrv就把动画和脚本联系起来了。
2、在程序中调用按钮,要打开“窗口“菜单里面的“组件“窗口,快捷键是Ctrl+F7在user interface中找到Button 然后拖到右边的库中,才能在程序中调用fl.controls.Button 因为这个小东西是组件。
3、下面做个Flash|+ JavaServlet的小的通讯Demo
as代码如下:
package{
import flash.display.Sprite;
import flash.text.TextField;
import fl.controls.Button;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.net.URLRequest;
import flash.net.URLLoader;
import flash.net.URLRequestMethod;
import flash.net.URLLoaderDataFormat;
import flash.events.IEventDispatcher;
import flash.net.URLVariables;
public class ConnectSrv extends Sprite{
public var connectBtn:Button;
public var msg:TextField;
public var _loader:URLLoader;
public function ConnectSrv(){
connectBtn = new Button();
connectBtn.x = 50;
connectBtn.y = 150;
connectBtn.addEventListener(MouseEvent.CLICK,onClickConBtn); //按钮的消息处理
addChild(connectBtn);
msg = new TextField();
msg.x = 100;
msg.y = 200;
msg.text = "成功啦"
addChild(msg);
}
public function onClickConBtn(e:MouseEvent){
_loader = new URLLoader();
_loader.dataFormat = URLLoaderDataFormat.VARIABLES;
configListeners(_loader);
var par:URLVariables = new URLVariables();
par.username = "alvetjook";
par.password = "2222";
var req:URLRequest = new URLRequest( "http://localhost:8080/flashT/Rev");
req.data = par;
req.method = URLRequestMethod.GET;
try {
_loader.load(req);
}catch(error:Error){
msg.text = "出错了";
}
}
public function configListeners(dispatcher:IEventDispatcher):void{
dispatcher.addEventListener(Event.COMPLETE,loadHandler);
}
public function loadHandler(e:*):void{
switch (e.type){
case Event.COMPLETE:
msg.text = "完成";
break;
}
}
}
}
import flash.display.Sprite;
import flash.text.TextField;
import fl.controls.Button;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.net.URLRequest;
import flash.net.URLLoader;
import flash.net.URLRequestMethod;
import flash.net.URLLoaderDataFormat;
import flash.events.IEventDispatcher;
import flash.net.URLVariables;
public class ConnectSrv extends Sprite{
public var connectBtn:Button;
public var msg:TextField;
public var _loader:URLLoader;
public function ConnectSrv(){
connectBtn = new Button();
connectBtn.x = 50;
connectBtn.y = 150;
connectBtn.addEventListener(MouseEvent.CLICK,onClickConBtn); //按钮的消息处理
addChild(connectBtn);
msg = new TextField();
msg.x = 100;
msg.y = 200;
msg.text = "成功啦"
addChild(msg);
}
public function onClickConBtn(e:MouseEvent){
_loader = new URLLoader();
_loader.dataFormat = URLLoaderDataFormat.VARIABLES;
configListeners(_loader);
var par:URLVariables = new URLVariables();
par.username = "alvetjook";
par.password = "2222";
var req:URLRequest = new URLRequest( "http://localhost:8080/flashT/Rev");
req.data = par;
req.method = URLRequestMethod.GET;
try {
_loader.load(req);
}catch(error:Error){
msg.text = "出错了";
}
}
public function configListeners(dispatcher:IEventDispatcher):void{
dispatcher.addEventListener(Event.COMPLETE,loadHandler);
}
public function loadHandler(e:*):void{
switch (e.type){
case Event.COMPLETE:
msg.text = "完成";
break;
}
}
}
}
服务器端Java代码:
package cn.com.sun.flash;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class RecieveXML extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
//super.doGet(arg0, arg1);
String data = req.getParameter( "username");
String pwd = req.getParameter( "password");
System.out.println(data);
System.out.println(pwd);
BufferedReader bfReader = req.getReader();
if (bfReader!= null){
String inputString = bfReader.readLine();
while (inputString!= null)
System.out.println(inputString);
bfReader.close();
}
System.out.println( "enen.....");
PrintWriter pw = resp.getWriter();
pw.print( "status = ok"); //关于这里以后有话要说
pw.flush();
pw.close();
}
@Override
protected void doPost(HttpServletRequest arg0, HttpServletResponse arg1) throws ServletException, IOException {
// TODO Auto-generated method stub
System.out.println( "posting....");
doGet(arg0, arg1);
}
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class RecieveXML extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
//super.doGet(arg0, arg1);
String data = req.getParameter( "username");
String pwd = req.getParameter( "password");
System.out.println(data);
System.out.println(pwd);
BufferedReader bfReader = req.getReader();
if (bfReader!= null){
String inputString = bfReader.readLine();
while (inputString!= null)
System.out.println(inputString);
bfReader.close();
}
System.out.println( "enen.....");
PrintWriter pw = resp.getWriter();
pw.print( "status = ok"); //关于这里以后有话要说
pw.flush();
pw.close();
}
@Override
protected void doPost(HttpServletRequest arg0, HttpServletResponse arg1) throws ServletException, IOException {
// TODO Auto-generated method stub
System.out.println( "posting....");
doGet(arg0, arg1);
}
}
代码都很简单,有点程序基础的都应该能看懂
as 里面有这么一句话
URLLoaderDataFormat.VARIABLES
这句话当时让我郁闷很久
一直报下面这个错误
Error: Error #2101: 传递给 URLVariables.decode() 的字符串必须是包含
名称/值对的
URL 编码的查询字符串。
at Error$/throwError()
at flash.net::URLVariables/decode()
at flash.net::URLVariables$iinit()
at flash.net::URLLoader/flash.net:URLLoader::onComplete()
后来查资料得出结论,URLLoaderDataFormat.VARIABLES是要从服务器读东西的
at Error$/throwError()
at flash.net::URLVariables/decode()
at flash.net::URLVariables$iinit()
at flash.net::URLLoader/flash.net:URLLoader::onComplete()
后来查资料得出结论,URLLoaderDataFormat.VARIABLES是要从服务器读东西的
pw.print(
"status = ok");
也可以把VARIABLES,改成Text 就没事了
4.安全沙箱
安全性较高,但也让我郁闷很久
好了,不废话,给出解决方案
windows安装目录下System32
\Macromed\Flash\FlashPlayerTrust建个文本文件,里面放上你的swf的绝对路径
或者绝对目录名即可,比如我的c:\project_fla\ConnectSrv.swf
这些问题都解决了,那么点击按钮应该出现"完成",程序就运行成功了