示例代码下载:
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;
}
}