Flex's HTTPService与JAVA 数据交互实例

来源:互联网 发布:剑三买金淘宝 编辑:程序博客网 时间:2024/04/29 11:29
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代码
  1. xmlHttpService.JSP

  2. <%@page import="quickstart.ShippingCalculator,
  3.                 quickstart.ShippingOption,
  4.                 java.util.List" %>

  5. <?xml version="1.0" encoding="utf-8"?>
  6. <options>
  7. <%
  8.     ShippingCalculator calc = new ShippingCalculator();
  9.     List    options;
  10.     int     zipcode;
  11.     double  pounds;

  12.     zipcode = Integer.parseInt(request.getParameter("zipcode"));
  13.     pounds = Double.parseDouble(request.getParameter("pounds"));

  14.     options = calc.getShippingOptions(zipcode, pounds);

  15.     for (int i = 0; i < options.size(); i++) {
  16.         ShippingOption option = (ShippingOption) options.get(i);
  17. %>
  18.     <option>
  19.         <service><%= option.getService() %></service>
  20.         <price><%= option.getPrice() %></price>
  21.     </option>
  22. <%
  23.     }
  24. %>
  25. </options>
  1. ShippingCalculator.java

  2. package quickstart;

  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import java.lang.Math;

  6. public class ShippingCalculator {

  7.     /*  Returns a list of made-up ShippingOptions.
  8.     */
  9.     public List getShippingOptions(int zipcode, double pounds) {

  10.         List        options = new ArrayList();
  11.         double      baseCost;

  12.         baseCost = Math.round(zipcode / 10000) + (pounds * 5);

  13.         options.add(new ShippingOption("Next Day", baseCost * 4));
  14.         options.add(new ShippingOption("Two Day Air", baseCost * 2));
  15.         options.add(new ShippingOption("Saver Ground", baseCost));

  16.         return options;
  17.     }
  18. }
  1. ShippingOption.java

  2. package quickstart;

  3. public class ShippingOption {

  4.     private String  service;
  5.     private double  price;

  6.     public ShippingOption() {
  7.     }

  8.     public ShippingOption(String aService, double aPrice) {
  9.         this.service = aService;
  10.         this.price = aPrice;
  11.     }

  12.     public void setService(String value) {
  13.         this.service = value;
  14.     }

  15.     public void setPrice(double value) {
  16.         this.price = value;
  17.     }

  18.     public String getService() { return this.service; }

  19.     public double getPrice() { return this.price; }
  20. }
------------------------------------------

代码是在网上看到的,以前总是很纳闷儿Flex到底怎么和其他语言交互的,看了这段代码总算明白了,简单的说,也就是
后台语言先把要显示的数据用XML的形式表示出来,然后Flex再利用
TTPService组件读取这个XML

,这样就达到了语言互通的作用,可能我的理解有点浅显,还望高手赐教