Flex亲密接触java

 flex和java工程可以集成到一起。但这个动作,至少要下载eclipse的plug-in。我要演示的是在flex builder3中建立独立的flex工程,然后在eclipse里建立独立的webservice服务。两者松散耦合,只需要webservice接口连接。

直接进入主题,看看java的webservice。

定义一个接口和一个实现:

  1. package mars.flex;
  2. public interface ITest {
  3.     
  4.     public String test();
  5.     public String sum(int a,int b);
  6.     public String sum1(String[] s);
  7.     
  8. }
  1. package mars.flex;
  2. public class Test implements ITest {
  3.     public String sum1(String[] ss) {
  4.         int result = 0;
  5.         for (String s : ss) {
  6.             int n = 0;
  7.             try {
  8.                 n = Integer.parseInt(s);
  9.                 result += n;
  10.             } catch (Exception e) {
  11.             }
  12.         }
  13.         return "the sum is:"+result;
  14.     }
  15.     
  16.     public String sum(int a,int b) {
  17.         int sum=a+b;
  18.         return "the sum is:"+sum;
  19.     }
  20.     public String test() {      
  21.         return "java webservice welcome!";
  22.     }
  23. }

发布到tomcat下,地址为:http://localhost:8888/ws4flex/services/ws4flex?wsdl

  1. <wsdl:definitions targetNamespace="http://flex.mars">
  2. <wsdl:types>
  3. <xsd:schema attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="http://flex.mars">
  4. <xsd:element name="sum">
  5. <xsd:complexType>
  6. <xsd:sequence>
  7. <xsd:element maxOccurs="1" minOccurs="1" name="in0" type="xsd:int"/>
  8. <xsd:element maxOccurs="1" minOccurs="1" name="in1" type="xsd:int"/>
  9. xsd:sequence>
  10. xsd:complexType>
  11. xsd:element>
  12. <xsd:element name="sumResponse">
  13. <xsd:complexType>
  14. <xsd:sequence>
  15. <xsd:element maxOccurs="1" minOccurs="1" name="out" nillable="true" type="xsd:string"/>
  16. xsd:sequence>
  17. xsd:complexType>
  18. xsd:element>
  19. <xsd:element name="test">
  20. <xsd:complexType/>
  21. xsd:element>
  22. <xsd:element name="testResponse">
  23. <xsd:complexType>
  24. <xsd:sequence>
  25. <xsd:element maxOccurs="1" minOccurs="1" name="out" nillable="true" type="xsd:string"/>
  26. xsd:sequence>
  27. xsd:complexType>
  28. xsd:element>
  29. <xsd:complexType name="ArrayOfString">
  30. <xsd:sequence>
  31. <xsd:element maxOccurs="unbounded" minOccurs="0" name="string" nillable="true" type="xsd:string"/>
  32. xsd:sequence>
  33. xsd:complexType>
  34. <xsd:element name="sum1">
  35. <xsd:complexType>
  36. <xsd:sequence>
  37. <xsd:element maxOccurs="1" minOccurs="1" name="in0" nillable="true" type="tns:ArrayOfString"/>
  38. xsd:sequence>
  39. xsd:complexType>
  40. xsd:element>
  41. <xsd:element name="sum1Response">
  42. <xsd:complexType>
  43. <xsd:sequence>
  44. <xsd:element maxOccurs="1" minOccurs="1" name="out" nillable="true" type="xsd:string"/>
  45. xsd:sequence>
  46. xsd:complexType>
  47. xsd:element>
  48. xsd:schema>
  49. wsdl:types>
  50. <wsdl:message name="sumRequest">
  51. <wsdl:part name="parameters" element="tns:sum">
  52.     wsdl:part>
  53. wsdl:message>
  54. <wsdl:message name="sumResponse">
  55. <wsdl:part name="parameters" element="tns:sumResponse">
  56.     wsdl:part>
  57. wsdl:message>
  58. <wsdl:message name="sum1Request">
  59. <wsdl:part name="parameters" element="tns:sum1">
  60.     wsdl:part>
  61. wsdl:message>
  62. <wsdl:message name="testResponse">
  63. <wsdl:part name="parameters" element="tns:testResponse">
  64.     wsdl:part>
  65. wsdl:message>
  66. <wsdl:message name="sum1Response">
  67. <wsdl:part name="parameters" element="tns:sum1Response">
  68.     wsdl:part>
  69. wsdl:message>
  70. <wsdl:message name="testRequest">
  71. <wsdl:part name="parameters" element="tns:test">
  72.     wsdl:part>
  73. wsdl:message>
  74. <wsdl:portType name="ws4flexPortType">
  75. <wsdl:operation name="sum">
  76. <wsdl:input name="sumRequest" message="tns:sumRequest">
  77.     wsdl:input>
  78. <wsdl:output name="sumResponse" message="tns:sumResponse">
  79.     wsdl:output>
  80. wsdl:operation>
  81. <wsdl:operation name="test">
  82. <wsdl:input name="testRequest" message="tns:testRequest">
  83.     wsdl:input>
  84. <wsdl:output name="testResponse" message="tns:testResponse">
  85.     wsdl:output>
  86. wsdl:operation>
  87. <wsdl:operation name="sum1">
  88. <wsdl:input name="sum1Request" message="tns:sum1Request">
  89.     wsdl:input>
  90. <wsdl:output name="sum1Response" message="tns:sum1Response">
  91.     wsdl:output>
  92. wsdl:operation>
  93. wsdl:portType>
  94. <wsdl:binding name="ws4flexHttpBinding" type="tns:ws4flexPortType">
  95. <wsdlsoap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
  96. <wsdl:operation name="sum">
  97. <wsdlsoap:operation soapAction=""/>
  98. <wsdl:input name="sumRequest">
  99. <wsdlsoap:body use="literal"/>
  100. wsdl:input>
  101. <wsdl:output name="sumResponse">
  102. <wsdlsoap:body use="literal"/>
  103. wsdl:output>
  104. wsdl:operation>
  105. <wsdl:operation name="test">
  106. <wsdlsoap:operation soapAction=""/>
  107. <wsdl:input name="testRequest">
  108. <wsdlsoap:body use="literal"/>
  109. wsdl:input>
  110. <wsdl:output name="testResponse">
  111. <wsdlsoap:body use="literal"/>
  112. wsdl:output>
  113. wsdl:operation>
  114. <wsdl:operation name="sum1">
  115. <wsdlsoap:operation soapAction=""/>
  116. <wsdl:input name="sum1Request">
  117. <wsdlsoap:body use="literal"/>
  118. wsdl:input>
  119. <wsdl:output name="sum1Response">
  120. <wsdlsoap:body use="literal"/>
  121. wsdl:output>
  122. wsdl:operation>
  123. wsdl:binding>
  124. <wsdl:service name="ws4flex">
  125. <wsdl:port name="ws4flexHttpPort" binding="tns:ws4flexHttpBinding">
  126. <wsdlsoap:address location="http://localhost:8888/ws4flex/services/ws4flex"/>
  127. wsdl:port>
  128. wsdl:service>
  129. wsdl:definitions>

这里定义了三个方法:

test()是最简单的测试通信。sum(int a,int b)测试传入两个参数时,通信问题。sum1(String[] s)测试数组参数是否能被正确传递。

 

下面,我们来看看flex工程。

  1. xml version="1.0" encoding="utf-8"?>
  2. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" fontSize="13">
  3.     <mx:Script>
  4.         
  5.             import mx.rpc.events.ResultEvent;//引用ResultEvent类
  6.             import mx.controls.Alert;//引用Alert类
  7.             import Model4ws;
  8.             private function wsHandle(e:ResultEvent):void
  9.             {
  10.                 lbl.text=e.result as String;//显示返回数据
  11.             }
  12.             private function testclk():void{
  13.                 ws4flex.test.send();
  14.             }
  15.             private function sumclk():void{
  16.                 ws4flex.sum.send();
  17.             }
  18.             private function sum1clk():void{
  19.                 ws4flex.sum1.send();
  20.             }
  21.         ]]>
  22.     mx:Script>    
  23.     
  24.     <mx:Panel title="和java的WebService交互" horizontalAlign="center" verticalAlign="middle" width="433" height="196">
  25.         <mx:Label id="given" textAlign="center" width="378" height="24" text=""/>
  26.         <mx:Label id="lbl" textAlign="center" width="378" height="24"/>
  27.     mx:Panel>
  28.     
  29.     <mx:WebService id="ws4flex" showBusyCursor="true"
  30.         wsdl="http://localhost:8888/ws4flex/services/ws4flex?wsdl"
  31.         fault="Alert.show(event.fault.faultString, 'Error')">
  32.         
  33.         <mx:operation name="test" result="wsHandle(event);">
  34.         mx:operation>
  35.         
  36.         <mx:operation name="sum" result="wsHandle(event);">
  37.         <mx:request>
  38.             <in0>1in0>
  39.             <in1>2in1>
  40.         mx:request>
  41.         mx:operation>
  42.         
  43.         <mx:operation name="sum1" result="wsHandle(event);">
  44.         <mx:request>
  45.             <in0>[1,2,3,4,5]in0>
  46.         mx:request>
  47.         mx:operation>     
  48.      mx:WebService>
  49.     <mx:Button x="57" y="226" label="test" click="testclk()" fillAlphas="[1.0, 1.0]" fillColors="[#1BD8AE, #1BD8AE]"/>
  50.     <mx:Button x="164" y="226" label="sum" click="sumclk()" fillAlphas="[1.0, 1.0]" fillColors="[#305EB3, #305EB3]" color="#BFDEE5"/>
  51.     <mx:Button x="281" y="226" label="sum1" click="sum1clk()" borderColor="#E9C935" fillAlphas="[1.0, 1.0]" fillColors="[#F05408, #F05408]"/>    
  52. mx:Application>

mx:WebService 用来定义webservice调用。mx:operation 定义webservice中调用的方法。mx:request 定义该方法需要哪些参数

 

最后看看效果

你可能感兴趣的:(9,Flex,flex,webservice,parameters,string,eclipse,java)