Flex3 CRUD 與Java後台互動 完整Demo

來源:互聯網
上載者:User

網上關於flex java curd的例子很少,官方的文檔不全且有錯誤.今天自己做的個crud的例子,
不帶分頁(分頁網上有很多例子了).
:

mxml:

<mx:Application    xmlns:mx="http://www.adobe.com/2006/mxml"    layout="absolute">   <mx:Script>        <![CDATA[            include "product.as";        ]]>    </mx:Script>    <mx:HTTPService        id="productService"        url="http://localhost:8888/flex2/productServlet"        resultFormat="e4x"        useProxy="false" />  <!--url改成您的資料請求地址-->    <mx:ViewStack id="viewstack1" width="731" height="473"  x="86.5" y="10">    <!--index 0 -->        <mx:Canvas label="Form View" width="100%" height="100%">            <mx:Form horizontalCenter="0" verticalCenter="0"                backgroundColor="#18E1CC" width="124" height="56">                    <mx:Button label="進入" click="fill()" width="100"/>            </mx:Form>        </mx:Canvas>    <!--index 1-->            <mx:Panel label="AdvancedDataGrid 顯示" width="100%" height="100%"  layout="absolute">            <mx:AdvancedDataGrid id="grid1" width="666" height="380" dataProvider="{_result.product}"  editable="true" itemEditEnd="updateHandler(event)"   x="10" y="10">                  <mx:columns>                     <mx:AdvancedDataGridColumn  dataField="id" headerText="ID"  editable="false"/>                    <mx:AdvancedDataGridColumn  dataField="productName" headerText="產品名稱" />                    <mx:AdvancedDataGridColumn  dataField="remark" headerText="備忘" />                  </mx:columns>            </mx:AdvancedDataGrid>                        <mx:Button x="60" y="401" label="添加" click="{viewstack1.selectedIndex = 2}"/>            <mx:Button label="刪除"   x="180" y="401" click="remove()"/>            </mx:Panel>    <!--index 2-->        <mx:Canvas label="添加新記錄" width="100%" height="100%" id="canvas3" >            <mx:Form                    backgroundColor="#FFFFFF"  verticalCenter="-91" horizontalCenter="-138">                                    <mx:FormItem label="" width="189" height="20">                    <mx:Button  label="返回" click="this.viewstack1.selectedIndex=1"/>                    <mx:TextInput id="hidden_id"  visible="false" />                </mx:FormItem>                                    <mx:FormItem label="產品名稱">                    <mx:TextInput id="productName"/>                </mx:FormItem>                                <mx:FormItem label="備忘">                    <mx:TextInput id="remark"/>                </mx:FormItem>                                <mx:Button label="儲存" click="insertProduct()" id="btn" />            </mx:Form>        </mx:Canvas>            </mx:ViewStack>   </mx:Application>

as:

import mx.collections.XMLListCollection;import mx.controls.Alert;import mx.controls.TextInput;import mx.events.AdvancedDataGridEvent;import mx.events.CloseEvent;import mx.rpc.events.ResultEvent;private var params:Object = new Object();//private var ld:XMLListCollection; 官方文檔的XMLListCollection並不能用,例子有問題鬱悶[Bindable]private var _result : XML ; //注意檔案名稱防止衝突/** * xml資料的渲染 * */public function resultHandler(event:ResultEvent):void {    _result = XML(event.result);}/** * 查詢所有產品的按鈕事件 * */public function insertItemHandler(event:ResultEvent):void {    fill();}/** * 查詢所有產品的方法  * */public function fill():void{    //為productService(HTTPService) 重新綁定監聽器(查詢)    productService.removeEventListener(ResultEvent.RESULT,insertItemHandler);    productService.addEventListener(ResultEvent.RESULT,resultHandler);    productService.method = "GET";    //要傳遞的參數    params['method'] = "findAll";    productService.cancel();    productService.send(params);    //切換到Grid視圖    viewstack1.selectedIndex=1;}/** * 插入產品 * */public function insertProduct():void{    //綁定新的監聽器(插入)    productService.removeEventListener(ResultEvent.RESULT,resultHandler);    productService.addEventListener(ResultEvent.RESULT,insertItemHandler);    productService.method = "POST";    //傳遞Form表單參數    params = {"method": "save", "id": NaN, "productName": productName.text,                 "remark": remark.text};    productService.cancel();    productService.send(params);    clearInputFields();}/** *  * 更新記錄的事件處理函數 *  * */public function updateHandler(event:AdvancedDataGridEvent):void{    //取消的話不更新    if(event.reason == "cancelled")    {        return;    }    //重新註冊    productService.removeEventListener(ResultEvent.RESULT,resultHandler);    productService.addEventListener(ResultEvent.RESULT,insertItemHandler);     //得到輸入後的新資料         var newData:String = (TextInput(event.currentTarget.itemEditorInstance)).text;     //得到輸入前的三個資料     var _id : int  = this.grid1.selectedItem["id"];     var _productName :String = this.grid1.selectedItem["productName"];     var _remark :String = this.grid1.selectedItem["remark"];     //第二列為產品名稱     if(event.columnIndex == 1)     {         _productName = newData;     }     //第三列為備忘     if(event.columnIndex == 2)     {         _remark = newData;     }     params = {"method": "update", "id": _id,"productName":_productName,"remark":_remark};     productService.cancel();     productService.send(params);}/** * 刪除的方法 * */public function remove() : void{    var index:int = this.grid1.selectedIndex;    if(index == -1)    {         Alert.show("您沒有選擇任何記錄","提示");         return;    }     Alert.yesLabel = "確定";     Alert.cancelLabel = "取消";     Alert.show("確定要刪除嗎?","提示",Alert.YES|Alert.CANCEL,this,defaultCloseHandler);}/** * 處理選擇是否刪除後的事件 *  * */public function defaultCloseHandler(event:CloseEvent):void{     //如果點擊了確定     if(event.detail == Alert.YES)     {          productService.removeEventListener(ResultEvent.RESULT,resultHandler);         productService.addEventListener(ResultEvent.RESULT,insertItemHandler);          var id : String  = this.grid1.selectedItem["id"];         params = {"method": "remove", "id": id};         productService.cancel();         productService.send(params);     }}/** * 清除form中的屬性值 * */private function clearInputFields():void{    productName.text = "";    remark.text = "";}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.