Flex(XML格式):
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
<mx:Script>
<![CDATA[
import mx.rpc.events.ResultEvent;
import mx.rpc.events.FaultEvent;
import mx.controls.Alert;
public function handleXML(event:ResultEvent):void
{
shippingInfo = event.result.option as XML;
}
public function handleFault(event:FaultEvent):void
{
Alert.show(event.fault.faultString, "Error");
}
]]>
</mx:Script>
<mx:HTTPService result="handleXML(event);" fault="handleFault(event);" id="xmlRPC" resultFormat="e4x"
method="POST" url="http://yourserver.com/xml/xmlHttpService.jsp" useProxy="false">
<mx:request xmlns="">
<zipcode>{zipcode.text}</zipcode>
<pounds>{weight_lb.text}</pounds>
</mx:request>
</mx:HTTPService>
<mx:Label x="56" y="32" text="Zip Code" width="55" height="18" textAlign="right" fontWeight="bold"/>
<mx:Label x="56" y="58" text="Weight" width="55" height="18" textAlign="right" fontWeight="bold"/>
<mx:TextInput x="130" y="32" id="zipcode" width="160" height="22"/>
<mx:TextInput x="130" y="58" id="weight_lb" width="160" height="22"/>
<mx:Button x="130" y="95" label="Get Shipping Options" click="xmlRPC.send();" width="160" height="22"/>
<mx:DataGrid
dataProvider="{shippingInfo}"
x="80" y="141" width="262" height="92" id="shippingOptionsList" editable="false" enabled="true">
<mx:columns>
<mx:DataGridColumn headerText="Service" dataField="service" />
<mx:DataGridColumn headerText="Price" dataField="price" />
</mx:columns>
</mx:DataGrid>
</mx:Application>
-------------------------------------------
JAVA代码
- xmlHttpService.JSP
- <%@page import="quickstart.ShippingCalculator,
- quickstart.ShippingOption,
- java.util.List" %>
- <?xml version="1.0" encoding="utf-8"?>
- <options>
- <%
- ShippingCalculator calc = new ShippingCalculator();
- List options;
- int zipcode;
- double pounds;
- zipcode = Integer.parseInt(request.getParameter("zipcode"));
- pounds = Double.parseDouble(request.getParameter("pounds"));
- options = calc.getShippingOptions(zipcode, pounds);
- for (int i = 0; i < options.size(); i++) {
- ShippingOption option = (ShippingOption) options.get(i);
- %>
- <option>
- <service><%= option.getService() %></service>
- <price><%= option.getPrice() %></price>
- </option>
- <%
- }
- %>
- </options>
- ShippingCalculator.java
- package quickstart;
- import java.util.ArrayList;
- import java.util.List;
- import java.lang.Math;
- public class ShippingCalculator {
-
- public List getShippingOptions(int zipcode, double pounds) {
- List options = new ArrayList();
- double baseCost;
- baseCost = Math.round(zipcode / 10000) + (pounds * 5);
- options.add(new ShippingOption("Next Day", baseCost * 4));
- options.add(new ShippingOption("Two Day Air", baseCost * 2));
- options.add(new ShippingOption("Saver Ground", baseCost));
- return options;
- }
- }
- ShippingOption.java
- package quickstart;
- public class ShippingOption {
- private String service;
- private double price;
- public ShippingOption() {
- }
- public ShippingOption(String aService, double aPrice) {
- this.service = aService;
- this.price = aPrice;
- }
- public void setService(String value) {
- this.service = value;
- }
- public void setPrice(double value) {
- this.price = value;
- }
- public String getService() { return this.service; }
- public double getPrice() { return this.price; }
- }
------------------------------------------
代码是在网上看到的,以前总是很纳闷儿Flex到底怎么和其他语言交互的,看了这段代码总算明白了,简单的说,也就是
后台语言先把要显示的数据用XML的形式表示出来,然后Flex再利用TTPService组件读取这个XML
,这样就达到了语言互通的作用,可能我的理解有点浅显,还望高手赐教