使用Cairngorm开发Flex.docx
- 文档编号:9459188
- 上传时间:2023-05-19
- 格式:DOCX
- 页数:30
- 大小:296.30KB
使用Cairngorm开发Flex.docx
《使用Cairngorm开发Flex.docx》由会员分享,可在线阅读,更多相关《使用Cairngorm开发Flex.docx(30页珍藏版)》请在冰点文库上搜索。
使用Cairngorm开发Flex
使用Cairngorm开发Flex
(一):
Cairngrom的构架
写flex也写了2年多了,居然还没有用过pureMVC或者Cairngrom这样的MVC框架,个人觉得有点丢人了。
大概看了一下,框架的结构和以前自己开发的时候差不多。
但是,虽然是一样,也不够专业啊,于是发了把狠,开始决定研究下Cairngorm,不能落后不是^^。
先了解下Cairngorm的分层思想:
什么是Cairngorm
cairngorm中文名是“烟水晶”,不知道“烟水晶”是啥东西,它是一个轻量级的FlexRIA程序开发框架,从而使程序的扩展性、可维护性都大大提高,不要把他看的有多神圣,个人认为其实就是一个开发的骨架,按这个骨架开发的话,可以实现三层解耦,各层次之间使用消息通讯。
Cairngorm主要是对开发flex应用程序加入了一系列的设计模式,从而使开发出来的flex程序可扩展性,可维护性的都大大提高。
Cairngorm的构架
借用一张别人的构架图:
可以看到主要有6个组件:
1.view
2.frontcontroller
3.command
4.businessdelegate
5.modellocator
6.servicelocator
现在来了解一下每一个组件都是负责什么工作的,结合上面的图会更加清晰:
1.view:
我们在这里保存自定义的视图组件,要使用modellocator中的数据,是cairngormevent事件产生的地方。
2.frontcontroller:
这个组件主要就是实现映射,他会监听cairngormevent,把响应映射到cairngormcommand里面的响应函数上。
这有这一个功能。
3.command:
业务逻辑层,功能核心,他负责更新modellocator,调用businessdelegate或者其他command
4.businessdelegate:
他负责http,webservice的方式的通信,由Command创建,返回结果给Command。
5.modellocator:
我们程序的运行时项目都保存在这里,必须是可绑定的,而且是单例。
6.servicelocator:
在这里定义远程访问的对象。
了解了这个框架的构架以后,下一篇文章会提供一个demo,结合demo就能明白cairngorm怎样在项目中被使用了。
这里我们继续Cairngorm的介绍,认识了什么是Cairngorm以后,我们来实现一个demo。
跟着一步步做先来的话,相信会对这个框架的使用有更深的理解。
需要准备:
在看这个例子之前,确保具备以下条件:
1.读懂了使用Cairngorm开发Flex
(一),理解Cairngorm的构架是怎样的。
2.具有一定的java知识。
3.知道flexsdk4。
例子的环境和工具:
1.Flexbuilder4。
下载很容易找到。
这个例子也是我用flex4写的第一个例子,发现跟flex3很多不同,要重新学习,痛苦的转型啊,泪奔~。
2.Mysql数据库。
3.tomcat。
4.blazeds。
不知道怎么配置的可以看我的文章:
例子功能描述:
功能很简单,在前端添加好友(名字,电话),将好友信息发送到java编写的后台,存储到数据库,成功的话返回所有好友到客户端显示。
数据库sql:
新建一个mysql数据库,名字叫:
CairngormDemo。
在数据库下执行下面sql建表:
[c-sharp] viewplaincopyprint?
1.CREATE TABLE `cairngormdemo`.`T_FriendInfo` (
2. `fid` INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
3. `fname` VARCHAR(45) NOT NULL,
4. `ftel` VARCHAR(45) NOT NULL,
5. PRIMARY KEY (`fid`)
6.)
7.ENGINE = InnoDB;
后台java:
项目结构:
MysqlConnection.java:
[java] viewplaincopyprint?
1.package com.gy.demo;
2.//导入sql类
3.import java.sql.Connection;
4.import java.sql.DriverManager;
5.public class MysqlConnection {
6. //定义静态的实例,该实例是程序运行时的唯一实例
7. private static MysqlConnection instance;
8.
9. private MysqlConnection() {
10. //调用数据库驱动
11. try {
12. Class.forName("com.mysql.jdbc.Driver");
13. }catch(ClassNotFoundException e) {
14. e.printStackTrace();
15. }
16. }
17. //对外接口,获得唯一实例
18. public static MysqlConnection GetInstance() {
19. if(instance == null) {
20. instance = new MysqlConnection();
21. }
22. return instance;
23. }
24. //获得数据库连接
25. public Connection GetConnection() throws java.sql.SQLException {
26. //定义数据库信息
27. String dbServer = "localhost";
28. String dbName = "CairngormDemo";
29. String dbUser = "root";
30. String dbPass = "860116";
31. //构造连接字符串,设置编码
32. String connStr = "jdbc:
mysql:
//"+dbServer+"/"+dbName+"?
user="+dbUser+"&password="+dbPass+"&useUnicode=true&characterEncoding=utf8";
33. //返回连接
34. return DriverManager.getConnection(connStr);
35. }
36.}
DemoService.java
[java] viewplaincopyprint?
1.package com.gy.demo;
2.import java.sql.Connection;
3.import java.sql.ResultSet;
4.import java.sql.SQLException;
5.import java.sql.Statement;
6.import java.util.ArrayList;
7.import java.util.HashMap;
8.import java.util.Map;
9.import com.gy.demo.MysqlConnection;
10.public class DemoService
11.{
12. public ArrayList
13. {
14. String sql = "insert into T_FriendInfo(fname,ftel) values(/'"+name+"/',/'"+tel+"/')";
15. System.out.println(sql);
16. int result = 0;
17. Connection conn = null;
18. try
19. {
20. conn = MysqlConnection.GetInstance().GetConnection();
21. Statement stmt = conn.createStatement();
22. result = stmt.executeUpdate(sql);
23. }
24. catch(SQLException e)
25. {
26. e.printStackTrace();
27. }
28. finally
29. {
30. try
31. {
32. conn.close();
33. }
34. catch(Exception e)
35. {
36. e.printStackTrace();
37. }
38. }
39. if(result==0) return null;
40. return this.GetAllFriends();
41. }
42.
43. public ArrayList
44. {
45. ArrayList
46. Connection conn = null;
47. try
48. {
49. conn = MysqlConnection.GetInstance().GetConnection();
50. Statement stmt = conn.createStatement();
51. String sql = "select * from T_FriendInfo";
52. ResultSet rs = stmt.executeQuery(sql);
53. Map
54. while(rs.next())
55. {
56. friend = new HashMap
57. friend.put("fname", rs.getString("fname"));
58. friend.put("ftel", rs.getString("ftel"));
59. friendArr.add(friend);
60. }
61. }
62. catch(SQLException e)
63. {
64. friendArr = null;
65. e.printStackTrace();
66. }
67. finally
68. {
69. try
70. {
71. conn.close();
72. }
73. catch(Exception e)
74. {
75. e.printStackTrace();
76. }
77. }
78. return friendArr;
79. }
80.}
ProgramEnter.java(测试用):
[c-sharp] viewplaincopyprint?
1.package com.gy.demo;
2.import java.util.ArrayList;
3.import java.util.Map;
4.public class ProgramEnter {
5. public static void main(String[] args)
6. {
7. DemoService ds = new DemoService();
8. ArrayList
9. if(friendInfo!
=null)
10. {
11. Map
12. System.out.println("fname:
"+f.get("fname")+";ftel:
"+f.get("ftel"));
13. }
14. }
15.}
可以运行测试程序来测试是否正确。
重头戏Flex:
启动flexbuilder4,新建个项目,这个项目依然使用到blazeds,相关环境的配置看
第一步:
新建项目,注意选择服务器类型为j2ee。
第二步:
配置服务器,这里不清楚的看我之前关于环境配置那片文章就行了。
第三步:
这里点添加swc,把我们的cairngorm包加进来。
第四步:
工程建好以后,建立以下的文件结构:
现在贴上每部分的代码:
名称:
FriendVO.as。
说明:
对象建模,对应数据库里的对象。
[c-sharp] viewplaincopyprint?
1.package com.gy.demo.vo
2.{
3. public class FriendVO
4. {
5. public var name:
String;
6. public var tel:
String;
7. }
8.}
名称:
AppModelLocator.as。
说明:
对应cairngorm框架里的modellocator模块,保存运行时数据,必须可绑定。
[c-sharp] viewplaincopyprint?
1.package com.gy.demo.model
2.{
3. import com.adobe.cairngorm.model.ModelLocator;
4.
5. import mx.collections.ArrayCollection;
6.
7. public class AppModelLocator implements ModelLocator
8. {
9. private static var _instance:
AppModelLocator;
10.
11. [Bindable]
12. public var friendsArr:
ArrayCollection = new ArrayCollection();
13.
14. public static function GetInstance():
AppModelLocator
15. {
16. if(_instance==null)
17. {
18. _instance = new AppModelLocator();
19. }
20. return _instance;
21. }
22. }
23.}
名称:
AddFriendController.as
说明:
对应control模块,只负责将command与对应的时间绑定,这里把AddFriendsEvent事件和AddFriendCommand绑定。
[c-sharp] viewplaincopyprint?
1.package com.gy.demo.control
2.{
3. import com.adobe.cairngorm.control.FrontController;
4. import com.gy.demo.event.AddFriendEvent;
5. import mand.AddFriendCommand;
6.
7. public class AddFriendController extends FrontController
8. {
9. public function AddFriendController()
10. {
11. addCommand(AddFriendEvent.EVENT_ADD_FRIEND,AddFriendCommand);
12. }
13. }
14.}
名称:
GetFriendsController.as。
说明:
对应control模块,只负责将command与对应的时间绑定,这里把GetFriendsEvent事件和GetFriendsCommand绑定。
[c-sharp] viewplaincopyprint?
1.package com.gy.demo.control
2.{
3. import com.adobe.cairngorm.control.FrontController;
4. import com.gy.demo.event.GetFriendsEvent;
5. import mand.GetFriendsCommand;
6.
7. public class GetFriendsController extends FrontController
8. {
9. public function GetFriendsController()
10. {
11. addCommand(GetFriendsEvent.EVENT_GET_FRIEND,GetFriendsCommand);
12. }
13. }
14.}
名称:
AddFriendCommand.as。
说明:
捕获添加事件后的响应模块,这里负责调用delegate模块,还有得到返回结果的相应处理也在这里完成。
这里添加成功后返回所有朋友的信息,我们要更新modellocator。
[c-sharp] viewplaincopyprint?
1.package mand
2.{
3. import mands.ICommand;
4. import com.adobe.cairngorm.control.CairngormEvent;
5. import com.gy.demo.delegate.AddFriendDelegate;
6. import com.gy.demo.event.AddFriendEvent;
7. import com.gy.demo.model.AppModelLocator;
8. import com.gy.demo.vo.FriendVO;
9.
10. import mx.collections.ArrayCollection;
11. import mx.rpc.IResponder;
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 使用 Cairngorm 开发 Flex