雪湖小札@blogjava

          心情與技術文檔同行 http://hi.baidu.com/jingleq

          BlogJava 首頁 新隨筆 聯系 聚合 管理
            24 Posts :: 2 Stories :: 4 Comments :: 0 Trackbacks
          示例代碼下載:flex-struts.rar
          該代碼包含兩個項目,一個是web項目,一個是flex項目,使用workshop+flex builder2開發,主要演示了如下幾個特征:
          (1).flex獲取遠程url的xml數據,加載到DataGrid中。在解釋數據的時候,使用了actionscript的class.
          actionscript class文件代碼:
           package org.c2ome.flex.xml

           public class XmlDoc
           
          {
            private 
          var headStr :String = 
             
          "<?xml version='1.0' ?>" + 
             
          "<Msg>" + 
             
          "<data>";
            private 
          var footStr: String =
             
          "</data>" + 
             
          "</Msg>";
            private 
          var contentStr: String = "";

            public 
          function addData(key : String, value: String) : void {
             
          var nodeStr: String = 
              
          "<entry><key>" + key + "</key><value>" + value +"</value>" + "</entry>";
             contentStr 
          +=nodeStr;
            }

            public 
          function toXml() : XML {
             
          return new XML(headStr + contentStr + footStr);
            }

            public 
          function toString() :String {
             
          return headStr + contentStr + footStr;
            }
           
           }

          }

          flexj.mxml代碼:

          <?xml version="1.0" encoding="utf-8"?>
          <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
           
          <mx:Script>
            
          <![CDATA[
            import org.c2ome.flex.xml.XmlDoc;
            import mx.rpc.events.ResultEvent;
            import mx.controls.Alert;
            import flash.events.Event;
            import flash.net.URLLoader;
            import flash.net.URLRequest;
                  private function loadApp():void {
             var loader:URLLoader = new URLLoader();
             var request:URLRequest = new URLRequest("http://localhost:8080/flexLogic.do?method=getBooks");
             var variables:URLVariables = new URLVariables();
             var x : XmlDoc = new XmlDoc();
             x.addData("name","c2one");
             x.addData("password","123456");
             variables.xmlDoc = x.toString();
             request.data = variables;
             request.method = "POST";
             loader.load(request);
             loader.addEventListener(Event.COMPLETE, onComplete);
                  }
            private function onComplete(event:Event):void
            {
                var loader:URLLoader = event.target as URLLoader;
                if (loader != null) {
                    var externalXML:XML = new XML(loader.data);
                    bookdg.dataProvider = externalXML.data.entry.(key=="cc").value.Book;
                 sysReturn.text = externalXML.data.entry.(key=="name").value.valueOf();
                }
                else {
                    Alert.show("loader is not a URLLoader!");
                }
            }
            
          ]]>
           
          </mx:Script>
           
          <mx:DataGrid x="78.5" y="93" id="bookdg">
            
          <mx:columns>
             
          <mx:DataGridColumn headerText="name" dataField="@name"/>
             
          <mx:DataGridColumn headerText="price" dataField="@price"/>
            
          </mx:columns>
           
          </mx:DataGrid>
           
          <mx:Button x="78.5" y="292" label="Button" click="loadApp();"/>
           
          <mx:TextArea x="78" y="256" height="28" width="202.5" id="sysReturn"/>
          </mx:Application>
          (2).在web端根目錄配置跨域訪問控制:建文件crossdomain.xml
          <?xml version="1.0"?> 
          <!DOCTYPE cross-domain-policy
          SYSTEM "http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd"
          >
          <cross-domain-policy>
              
          <allow-access-from domain="*" />
          </cross-domain-policy>
          (3).在web端使用了betwixt來處理xml與對象,看com.c2one.util.XMLUtil
          package com.c2one.util;

          import java.beans.IntrospectionException;
          import java.io.IOException;
          import java.io.StringReader;
          import java.io.StringWriter;
          import java.util.Map;

          import org.apache.commons.betwixt.io.BeanReader;
          import org.apache.commons.betwixt.io.BeanWriter;
          import org.xml.sax.SAXException;

          public class XMLUtil {

              
          public static String beanParser(String info, String exeCode,
                      Map
          <String, Object> data) throws IOException, SAXException,
                      IntrospectionException 
          {
                  String result 
          = "";
                  StringWriter outputWriter 
          = new StringWriter();
                  outputWriter.write(
          "<?xml version='1.0' ?>");
                  BeanWriter beanWriter 
          = new BeanWriter(outputWriter);
                  beanWriter.getXMLIntrospector().getConfiguration()
                          .setAttributesForPrimitives(
          true);
                  beanWriter.getBindingConfiguration().setMapIDs(
          false);
                  beanWriter.enablePrettyPrint();
                  ResponseEntity entity 
          = new ResponseEntity(info, exeCode, data);
                  beanWriter.write(
          "Msg", entity);
                  result 
          = outputWriter.toString();
                  outputWriter.close();
                  
          return result;
              }


              
          public static RequestEntity stringParser(String str)
                      
          throws IntrospectionException, IOException, SAXException {
                  StringReader xmlReader 
          = new StringReader(str);
                  BeanReader beanReader 
          = new BeanReader();
                  beanReader.getXMLIntrospector().getConfiguration()
                          .setAttributesForPrimitives(
          true);
                  beanReader.getBindingConfiguration().setMapIDs(
          false);
                  beanReader.registerBeanClass(
          "Msg", RequestEntity.class);
                  RequestEntity obj 
          = (RequestEntity) beanReader.parse(xmlReader);
                  
          return obj;
              }

          }
          posted on 2007-11-12 10:27 csnowfox 閱讀(2155) 評論(1)  編輯  收藏

          Feedback

          # re: struts與flex通過xml交互模型 2008-04-12 10:30 豆抓
          路過,哈  回復  更多評論
            


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 盈江县| 秀山| 舞阳县| 怀远县| 永顺县| 泰和县| 台中市| 兴海县| 西贡区| 白沙| 富川| 东源县| 闸北区| 象山县| 托克逊县| 耒阳市| 霍邱县| 华池县| 日照市| 太保市| 旌德县| 盐亭县| 汾阳市| 璧山县| 基隆市| 海伦市| 濮阳市| 攀枝花市| 二手房| 海淀区| 南通市| 新沂市| 蕲春县| 广饶县| 南充市| 长乐市| 鹰潭市| 沿河| 邵阳县| 满洲里市| 光山县|