flex基础篇二 flex加载数据的两种方式,httpService和本地xml
2013-10-24 10:34
609 查看
本次主要介绍flex获取数据的两种方式:
(1)httpService从后台获取,内容只能为xml格式。
发送httpService请求,httpService通信和java的ajax比较类似:
web.xml里面增加 servlet 配置:
后台servlet处理,并返回xml数据。
前台收到xml后,就能采用e4x来解析xml。下篇介绍如何使用 e4x。
(2)读取xml文件中的数据。因为flex有沙箱隔离,所以xml文件不能在本地,只能在flex的域里面。(就是在同一个服务器的部署目录下面)
1在.mxml文件中,采用<model>标签很容易就能读取到。网络上有很多介绍。
2在.as文件中读取xml
(1)httpService从后台获取,内容只能为xml格式。
发送httpService请求,httpService通信和java的ajax比较类似:
package com.boco.util.server { import flash.net.FileReference; import mx.controls.Alert; import mx.rpc.events.FaultEvent; import mx.rpc.events.ResultEvent; import mx.rpc.http.HTTPService; public class BasicPostHttpService { private var httpService:HTTPService; private var method:Function; //回调函数 public function BasicPostHttpService() { } public function getResultXML(xmlUrl:String,method:Function):void { this.method = method; //指定回调函数名称 httpService = new HTTPService(); //声明httpService对象 httpService.method="post"; //请求类型 httpService.useProxy = false; httpService.resultFormat = HTTPService.RESULT_FORMAT_E4X; //解析xml的方式 httpService.addEventListener(ResultEvent.RESULT,httpResult); //返回结果的处理函数 httpService.addEventListener(FaultEvent.FAULT,showResult); //出错处理函数 httpService.url = xmlUrl; //请求路径 httpService.send(); //发送 } public function showResult(event:FaultEvent):void{ Alert.show(event.fault.faultDetail); } public function httpResult(event:ResultEvent):void { resultXML(XML(event.result)); } public function resultXML(xml:XML):void { method.call(this,xml); //回调 } } }
web.xml里面增加 servlet 配置:
<servlet> <servlet-name>action</servlet-name> <servlet-class>com.boco.server.FlexServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>action</servlet-name> <url-pattern>*.flex</url-pattern> </servlet-mapping>
后台servlet处理,并返回xml数据。
//获取货架最高层数和最长列数 public void getSize(HttpServletRequest req,HttpServletResponse resp)throws ServletException, IOException{ StringBuffer sizeInfo = new StringBuffer(); Connection con =null; PreparedStatement pstmt = null; ResultSet rs = null; sizeInfo.append("<?xml version='1.0' encoding='UTF-8'?>"); sizeInfo.append("<size>"); try { con = DBUtil.getConnection(); String sql = "select (select max(childnum) from repository_location_table where type=2) as maxfloor,(select max(childnum) from repository_location_table where type=3) as maxline from dual"; pstmt = con.prepareCall(sql); rs = pstmt.executeQuery(); while(rs.next()){ sizeInfo.append("<maxWidth>"); sizeInfo.append(""+rs.getInt("MAXFLOOR")+""); sizeInfo.append("</maxWidth>"); sizeInfo.append("<maxHeight>"); sizeInfo.append(""+rs.getInt("MAXLINE")+""); sizeInfo.append("</maxHeight>"); } sizeInfo.append("</size>"); System.out.println(sizeInfo.toString()); } catch (Exception e) { e.printStackTrace(); } finally{ if(con!=null){ DBUtil.closeCon(con); } } resp.setCharacterEncoding("UTF-8"); resp.getWriter().print(sizeInfo); }
前台收到xml后,就能采用e4x来解析xml。下篇介绍如何使用 e4x。
(2)读取xml文件中的数据。因为flex有沙箱隔离,所以xml文件不能在本地,只能在flex的域里面。(就是在同一个服务器的部署目录下面)
1在.mxml文件中,采用<model>标签很容易就能读取到。网络上有很多介绍。
2在.as文件中读取xml
try{ //System.useCodePage = true; //默认未unicode编码,设置为true,按照文本的格式读取。 var xmlLoader:URLLoader = new URLLoader(); xmlLoader.load(new URLRequest("com/boco/draw/repository.xml")); xmlLoader.addEventListener(Event.COMPLETE,changeShowModel); }catch(error:Error){ Alert.show("read xml error"); }
public function changeShowModel(e:Event):void{ var xml:XML; var loader:URLLoader = e.target as URLLoader; if(loader!=null){ xml = new XML(loader.data); } handDetaiXML(xml);//这个方法中就能处理xml了,一样的用e4x }
相关文章推荐
- Flex 隐藏组件的属性
- Flex 如何得到itemRenderer里面的内容
- Flex字符串比较 还有Flex字符串操作
- Flex 透明效果,位于页面最底层
- Flex 非常实用的学习资料整理
- flex 控件的重要属性
- flex PopUpManager使用说明
- Flex clipContent 编程注意
- Flex 获得png透明截图的问题和解决方法
- FLEX TitleWindow之间数据传输的示例
- 在flex中执行一个javascript方法的简单方式
- Flex CategoryAxis 字体样式修改
- Flex结合JavaScript读取本地路径的方法
- Flex Namespace的用法
- Flex 性能优化常用手法总结
- flex 安全沙箱问题备忘
- Flex程序开发心得小结
- Flex Flash的关系分析
- AS3 Flex基础知识100条
- flex 优化技巧 收集[提升性能]