`
tof.j
  • 浏览: 132280 次
  • 性别: Icon_minigender_1
  • 来自: 宁波
社区版块
存档分类

flex和后端的数据交互(二)--<mx:request/>和<mx:form/>

阅读更多

转载:http://daoger.iteye.com/blog/205894

 

用actionscript给服务器请求添加参数难免会很麻烦,使用mx:request标签就可以解决这一问题,可以把他
嵌套到HTTPService标签中实现参数的提交。如下例所示:

Xml代码 复制代码
  1. <mx:request>  
  2.       <txtPara>{txtPara.text}</txtPara>  
  3.  </mx:request>  
<mx:request>
      <txtPara>{txtPara.text}</txtPara>
 </mx:request>


其中txtPara是发送到服务器端参数的名城,标签体是参数值,而标签体的值就是下方文本框的值。
以下是完整的mxml文件:

Xml代码 复制代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" >  
  3.     <mx:Script>     
  4.       <![CDATA[    
  5.         import mx.rpc.events.ResultEvent;                
  6.         import mx.controls.Alert;                        
  7.         private function httpHandle(e:ResultEvent):void   
  8.         {  
  9.             Alert.show(e.result.Result);  
  10.         }  
  11.         ]]>     
  12.     </mx:Script>     
  13.     <mx:HTTPService id="myHttp" url="http://localhost:8080/myflex/http" showBusyCursor="true" result="httpHandle(event);" useProxy="false">  
  14.         <mx:request>  
  15.             <txtPara>{txtPara.text}</txtPara>  
  16.         </mx:request>  
  17.     </mx:HTTPService>  
  18.     <mx:Panel title="TEST HTTPService" width="368" height="140" x="78" y="30" layout="absolute">  
  19.         <mx:Label text="PARA" x="110" y="26"/>     
  20.         <mx:TextInput id="txtPara" x="161" y="24" width="95"/>    
  21.         <mx:Label text="The para sent to service is:" x="58" y="53"/>  
  22.         <mx:Label x="126" y="53" id="lblResult"/>     
  23.         <mx:Button label="Submit" click="myHttp.send()" x="277" y="53"/>      
  24.     </mx:Panel>  
  25. </mx:Application>  
<?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.controls.Alert;                      
		private function httpHandle(e:ResultEvent):void 
		{
			Alert.show(e.result.Result);
		}
        ]]>  
  	</mx:Script>  
	<mx:HTTPService id="myHttp" url="http://localhost:8080/myflex/http" showBusyCursor="true" result="httpHandle(event);" useProxy="false">
		<mx:request>
         	<txtPara>{txtPara.text}</txtPara>
    	</mx:request>
	</mx:HTTPService>
	<mx:Panel title="TEST HTTPService" width="368" height="140" x="78" y="30" layout="absolute">
	   	<mx:Label text="PARA" x="110" y="26"/>  
	    <mx:TextInput id="txtPara" x="161" y="24" width="95"/> 
	    <mx:Label text="The para sent to service is:" x="58" y="53"/>
	    <mx:Label x="126" y="53" id="lblResult"/>  
	    <mx:Button label="Submit" click="myHttp.send()" x="277" y="53"/>   
	</mx:Panel>
</mx:Application>


在服务器端就可以从request中取到txtPara参数的值,这里没有做过多处理,只是在后端取到这个值又通过xml形式返还到客户端。
这是例子运行的截图:


mx:request组件一般是结合mx:form组件一起使用,flex提供了完备的数据校验功能,如对字符串的校验mx:StringValidator、
对电话号码验证的mx:PhoneNumberValidator、对日期验证的mx:DateValidator、对电子邮件验证的mx:EmailValidator、对邮编验证
的mx:ZipCodeValidator等等。下面这个示例来自Flex的在线文档,主要展示flex的form验证功能,没有数据的提交。

Xml代码 复制代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <!-- Simple example to demonstrate Form layout container. -->  
  3. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">  
  4.     <mx:Panel title="Form Container Example" height="75%" width="75%"    
  5.         paddingTop="10" paddingLeft="10" paddingRight="10" paddingBottom="10">  
  6.         <mx:Text width="100%" color="blue"  
  7.             text="Moving from one form field to another triggers the validator."/>  
  8.         <mx:Form width="100%" height="100%">  
  9.             <mx:FormHeading label="Enter values into the form."/>  
  10.             <mx:FormItem label="First name">  
  11.                 <mx:TextInput id="fname" width="200"/>  
  12.             </mx:FormItem>  
  13.             <mx:FormItem label="Date of birth (mm/dd/yyyy)">  
  14.                 <mx:TextInput id="dob" width="200"/>  
  15.             </mx:FormItem>  
  16.             <mx:FormItem label="E-mail address">  
  17.                 <mx:TextInput id="email" width="200"/>  
  18.             </mx:FormItem>  
  19.             <mx:FormItem label="Age">  
  20.                 <mx:TextInput id="age" width="200"/>  
  21.             </mx:FormItem>  
  22.             <mx:FormItem label="SSN">  
  23.                 <mx:TextInput id="ssn" width="200"/>  
  24.             </mx:FormItem>  
  25.             <mx:FormItem label="Zip">  
  26.                 <mx:TextInput id="zip" width="200"/>  
  27.             </mx:FormItem>  
  28.             <mx:FormItem label="Phone">  
  29.                 <mx:TextInput id="phone" width="200"/>  
  30.             </mx:FormItem>  
  31.         </mx:Form>  
  32.     </mx:Panel>  
  33.   
  34.     <mx:StringValidator source="{fname}" property="text" minLength="4" maxLength="12"/>  
  35.     <mx:PhoneNumberValidator source="{phone}" property="text"/>  
  36.     <mx:DateValidator source="{dob}" property="text"/>  
  37.     <mx:EmailValidator source="{email}" property="text"/>  
  38.     <mx:NumberValidator source="{age}" property="text" integerError="Enter Integer value"  
  39.         minValue="18" maxValue="100" domain="int"/>  
  40.     <mx:SocialSecurityValidator source="{ssn}" property="text"/>  
  41.     <mx:ZipCodeValidator source="{zip}" property="text"/>  
  42. </mx:Application>  
<?xml version="1.0" encoding="utf-8"?>
<!-- Simple example to demonstrate Form layout container. -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
    <mx:Panel title="Form Container Example" height="75%" width="75%" 
        paddingTop="10" paddingLeft="10" paddingRight="10" paddingBottom="10">
        <mx:Text width="100%" color="blue"
            text="Moving from one form field to another triggers the validator."/>
        <mx:Form width="100%" height="100%">
            <mx:FormHeading label="Enter values into the form."/>
            <mx:FormItem label="First name">
                <mx:TextInput id="fname" width="200"/>
            </mx:FormItem>
            <mx:FormItem label="Date of birth (mm/dd/yyyy)">
                <mx:TextInput id="dob" width="200"/>
            </mx:FormItem>
            <mx:FormItem label="E-mail address">
                <mx:TextInput id="email" width="200"/>
            </mx:FormItem>
            <mx:FormItem label="Age">
                <mx:TextInput id="age" width="200"/>
            </mx:FormItem>
            <mx:FormItem label="SSN">
                <mx:TextInput id="ssn" width="200"/>
            </mx:FormItem>
            <mx:FormItem label="Zip">
                <mx:TextInput id="zip" width="200"/>
            </mx:FormItem>
            <mx:FormItem label="Phone">
                <mx:TextInput id="phone" width="200"/>
            </mx:FormItem>
        </mx:Form>
    </mx:Panel>

    <mx:StringValidator source="{fname}" property="text" minLength="4" maxLength="12"/>
    <mx:PhoneNumberValidator source="{phone}" property="text"/>
    <mx:DateValidator source="{dob}" property="text"/>
    <mx:EmailValidator source="{email}" property="text"/>
    <mx:NumberValidator source="{age}" property="text" integerError="Enter Integer value"
        minValue="18" maxValue="100" domain="int"/>
    <mx:SocialSecurityValidator source="{ssn}" property="text"/>
    <mx:ZipCodeValidator source="{zip}" property="text"/>
</mx:Application>


示例运行的截图:


其他一些入门例子可以参考:甘先生Blog http://www.blogjava.net/gump/articles/22859.html
更多问题请参考Flex的在线文档:http://www.adobe.com/support/documentation/en/flex/

分享到:
评论

相关推荐

    flex 初学者入门资料

    &lt;mx:request&gt; &lt;daysBack&gt;30&lt;/daysBack&gt; &lt;limit&gt;{cbxNumPosts.value}&lt;/limit&gt; &lt;/mx:request&gt; &lt;/mx:operation&gt; &lt;/mx:WebService&gt; 发送请求:wsBlogAggr.getMostPopularPosts.send() 获取结果:wsBlogAggr....

    flex与java通信,通过插件blazed

    --flex与webservice交互这里调用一个天气预报的webservice--&gt; &lt;s:WebService id="ws" wsdl="http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl" fault="ws_faultHandler(event)" result="ws_...

    flex柱状图动态切换数据源实例

    该flex应用程序演示了柱状图动态切换数据源 &lt;mx:ColumnChart x="6" y="65" id="columnchart1" showDataTips="true" dataProvider="{list}" height="390" itemClick="onItemClick(event)"&gt; &lt;mx:horizontalAxis&gt; ...

    Flex Olap完整项目源码,可直接运行

    &lt;mx:Script source="include/OLAPAppInFlex.as" /&gt; &lt;mx:Script source="include/Chart.as" /&gt; &lt;mx:Script source="include/FlexBIDataGrid.as" /&gt; &lt;mx:Script source="include/OLAPGridConfigure.as" /&gt; &lt;mx:Style ...

    flex开发对时间控制

    flex 对时间的关注&lt;mx:Script&gt; &lt;![CDATA[ // Event handler for the DateField change event. private function dateChanged(date:Date):void { if (date == null) selection.text = "Date selected: "; ...

    Flex 完全自学手册

    &lt;mx:Panel width="262" height="291" layout="absolute" title="ʹԃMXML¶¨ӥة¼�&lt;mx:FormItem label="ѕ Ļ" x="17.5" y="70" width="206"&gt; &lt;mx:TextInput id="sName" width="127"/&gt; &lt;/mx:FormItem&gt; &lt;mx:...

    flex导出excel的代码

    &lt;mx:Script&gt; &lt;![CDATA[ import mx.controls.CheckBox; import mx.controls.Alert; import com.as3xls.xls.ExcelFile; import com.as3xls.xls.Sheet; import flash.filesystem.*; [Bindable] private ...

    《Flex第一步》书中源代码1

    &lt;name&gt;Example_1&lt;/name&gt; &lt;comment&gt;&lt;/comment&gt; &lt;projects&gt; &lt;/projects&gt; &lt;buildSpec&gt; &lt;buildCommand&gt; &lt;name&gt;com.adobe.flexbuilder.project.flexbuilder&lt;/name&gt; &lt;arguments&gt; &lt;/arguments&gt; &lt;/build...

    flexjava交互

    &lt;mx:Script&gt; &lt;![CDATA[ import mx.rpc.events.ResultEvent; import mx.controls.Alert; [Bindable] var result:String; function sendRequest(){ var name=name1.text; var password=password1.text; ro....

    flex实列demo

    &lt;mx:Panel width="100%" height="100%" title="Content" marginTop="1" marginBottom="1" marginLeft="1" marginRight="1" &gt; &lt;IFrame id="iFrame" source=...

    本地播放器源码仅供交流

    一个本地播放器的源码,学习学习 ... &lt;content&gt;[This value will be overwritten by Flex Builder in the output app.xml]&lt;/content&gt; &lt;visible&gt;true&lt;/visible&gt; &lt;/initialWindow&gt; &lt;/application&gt;

    springgraph.zip

    一个n的flex组件(SpringGraph Flex Component) SpringGraph Flex Component 有几个demo,不多做解释,用了就知道强大了,关于如何使用,以后在慢慢写了。 &lt;A href="http://mark-shepherd.com/thesaurus"&gt;...

    flex spy flex调试工具包

    使用方法如下: &lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"&gt; &lt;mx:Script&gt; &lt;![CDATA[ import mx.core.FlexSprite;...&lt;/mx:Application&gt;

    Flex调用xml通过DataGrid遍历简单示例

    &lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" creationComplete="service.send()" &gt; &lt;mx:Script&gt; &lt;![CDATA[ import mx.controls....&lt;/mx:Application&gt;

    flex examples

    &lt;?xml version="1.0" encoding="iso-8859-1"?&gt; &lt;mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml" ... &lt;mx:Script source="../constants.as"/&gt; &lt;mx:Canvas width="100%" horizontalScrollPolicy="of

    flex4cookbook

    pdf文件,书为英文版,内附源码. &lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" ...&lt;/s:Application&gt;

    Flex使用<mx:Tree>控件创建树(可添加和删除节点)

    NULL 博文链接:https://tongqiuyan.iteye.com/blog/1219351

    flex fusionchart 破解

    4.找到fusioncharts文件中FusionChartsFlex\Charts\FlashBuilder4_SWC下面的那个swc文件,将其复制到flex-libs文件夹下: 5.将FusionChartsFlex\Charts下面的FusionCharts和FusionWidgets两个文件夹复制到flex_...

    flex播放mp3

    flex播放mp3源码: &lt;s:BorderContainer x="131" y="147" width="369" height="108"&gt; &lt;s:Label x="83" y="47" text="进度:" width="45"/&gt; &lt;s:HSlider id="hslider1" x="135" y="48" width="221" changeEnd=...

Global site tag (gtag.js) - Google Analytics