加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Flex3 CRUD 与Java后台交互 完整Demo

发布时间:2020-12-15 04:29:23 所属栏目:百科 来源:网络整理
导读:网上关于flex java curd的例子很少,官方的文档不全且有错误.今天自己做的个crud的例子, 不带分页(分页网上有很多例子了). 上图: mxml: [html] ? view plain copy mx:Application ?? ???? xmlns:mx = "http://www.adobe.com/2006/mxml" ?? ???? layout = "abs

网上关于flex java curd的例子很少,官方的文档不全且有错误.今天自己做的个crud的例子,
不带分页(分页网上有很多例子了).
上图:

mxml:

[html]? view plain copy
  1. <mx:Application??
  2. ????xmlns:mx="http://www.adobe.com/2006/mxml"??
  3. ????layout="absolute">??
  4. ??
  5. ???mx:Script ????????<![CDATA[?
  6. ????????????include?"product.as";?
  7. ????????]]>??
  8. ????</mx:HTTPService??
  9. ????????id="productService"??
  10. ????????url="http://localhost:8888/flex2/productServlet"??
  11. ????????resultFormat="e4x"??
  12. ????????useProxy="false"?/>??<!--url改成您的数据请求地址-->??
  13. mx:ViewStack?id="viewstack1"?width="731"?height="473"??x="86.5"?y="10" ????<!--index?0?-->??
  14. ????????mx:Canvas?label="Form?View"?width="100%"?height="100%" ????????????mx:Form?horizontalCenter="0"?verticalCenter="0"??
  15. ????????????????backgroundColor="#18E1CC"?width="124"?height="56" ????????????????????mx:Button?label="进入"?click="fill()"?width="100"/>??
  16. ????????????mx:Form ????????mx:Canvas>??
  17. ????<!--index?1-->??????
  18. 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"/>??
  19. mx:AdvancedDataGridColumn??dataField="productName"?headerText="产品名称"? ????????????????????mx:AdvancedDataGridColumn??dataField="remark"?headerText="备注"?mx:AdvancedDataGrid ??????????????
  20. mx:Button?x="60"?y="401"?label="添加"?click="{viewstack1.selectedIndex?=?2}"mx:Button?label="删除"???x="180"?y="401"?click="remove()"/>??????
  21. mx:Panel<!--index?2-->??
  22. mx:Canvas?label="添加新记录"?width="100%"?height="100%"?id="canvas3"?mx:Form??
  23. ????????????????????backgroundColor="#FFFFFF"??verticalCenter="-91"?horizontalCenter="-138" ??????????????????????
  24. ????????????????mx:FormItem?label=""?width="189"?height="20"mx:Button??label="返回"?click="this.viewstack1.selectedIndex=1"mx:TextInput?id="hidden_id"??visible="false"? ????????????????mx:FormItem>??????
  25. ??????????????????
  26. mx:FormItem?label="产品名称"mx:TextInput?id="productName"mx:FormItem?label="备注"mx:TextInput?id="remark"mx:Button?label="保存"?click="insertProduct()"?id="btn"? ??????????
  27. ????mx:ViewStack ??
  28. ?????
  29. mx:Application>??


as:

copy
    import?mx.collections.XMLListCollection;??
  1. import?mx.controls.Alert;??
  2. import?mx.controls.TextInput;??
  3. import?mx.events.AdvancedDataGridEvent;??
  4. import?mx.events.CloseEvent;??
  5. import?mx.rpc.events.ResultEvent;??
  6. private?var?params:Object?=?new?Object();??
  7. //private?var?ld:XMLListCollection;?官方文档的XMLListCollection并不能用,例子有问题郁闷??
  8. [Bindable]??
  9. private?var?_result?:?XML?;?//注意文件名防止冲突??
  10. /**??
  11. ?*?xml数据的渲染??
  12. ?*?*/??
  13. public?function?resultHandler(event:ResultEvent):void???
  14. {??
  15. ????_result?=?XML(event.result);??
  16. }??
  17. /**??
  18. ?*?查询所有产品的按钮事件??
  19. ?*?*/??
  20. public?function?insertItemHandler(event:ResultEvent):void???
  21. {??
  22. ????fill();??
  23. }??
  24. ?*?查询所有产品的方法???
  25. public?function?fill():void??
  26. ????//为productService(HTTPService)?重新绑定监听器(查询)??
  27. ????productService.removeEventListener(ResultEvent.RESULT,insertItemHandler);??
  28. ????productService.addEventListener(ResultEvent.RESULT,resultHandler);??
  29. ????productService.method?=?"GET";??
  30. ????//要传递的参数??
  31. ????params['method']?=?"findAll";??
  32. ????productService.cancel();??
  33. ????productService.send(params);??
  34. ????//切换到Grid视图??
  35. ????viewstack1.selectedIndex=1;??
  36. ?*?插入产品??
  37. public?function?insertProduct():void??
  38. ????//绑定新的监听器(插入)??
  39. ????productService.method?=?"POST";??
  40. ????//传递Form表单参数??
  41. ????params?=?{"method":?"save",?"id":?NaN,?"productName":?productName.text,??
  42. ?????????????????"remark":?remark.text};??
  43. ????productService.cancel();??
  44. ????productService.send(params);??
  45. ????clearInputFields();??
  46. ?*???
  47. ?*?更新记录的事件处理函数??
  48. public?function?updateHandler(event:AdvancedDataGridEvent):void??
  49. ????//取消的话不更新??
  50. ????if(event.reason?==?"cancelled")??
  51. ????{??
  52. ????????return;??
  53. ????}??
  54. ????//重新注册??
  55. ????productService.removeEventListener(ResultEvent.RESULT,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> ????productService.addEventListener(ResultEvent.RESULT,248); line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> ?????//得到输入后的新数据??????
  56. ?????var?newData:String?=?(TextInput(event.currentTarget.itemEditorInstance)).text;??
  57. ?????//得到输入前的三个数据??
  58. ?????var?_id?:?int??=?this.grid1.selectedItem["id"];??
  59. ?????var?_productName?:String?=?this.grid1.selectedItem["productName"];??
  60. ?????var?_remark?:String?=?this.grid1.selectedItem["remark"];??
  61. ?????//第二列为产品名称??
  62. ?????if(event.columnIndex?==?1)??
  63. ?????{??
  64. ?????????_productName?=?newData;??
  65. ?????}??
  66. ?????//第三列为备注??
  67. ?????if(event.columnIndex?==?2)??
  68. ?????{??
  69. ?????????_remark?=?newData;??
  70. ?????}??
  71. ?????params?=?{"method":?"update",?"id":?_id,"productName":_productName,"remark":_remark};??
  72. ?????productService.cancel();??
  73. ?????productService.send(params);??
  74. ?*?删除的方法??
  75. public?function?remove()?:?void??
  76. ????var?index:int?=?this.grid1.selectedIndex;??
  77. ????if(index?==?-1)??
  78. ?????????Alert.show("您没有选择任何记录","提示");??
  79. ?????????return;??
  80. ????}??
  81. ?????Alert.yesLabel?=?"确定";??
  82. ?????Alert.cancelLabel?=?"取消";??
  83. ?????Alert.show("确定要删除吗?","提示",Alert.YES|Alert.CANCEL,this,defaultCloseHandler);??
  84. ?*?处理选择是否删除后的事件??
  85. public?function?defaultCloseHandler(event:CloseEvent):void??
  86. ?????//如果点击了确定??
  87. ?????if(event.detail?==?Alert.YES)??
  88. ??????????productService.removeEventListener(ResultEvent.RESULT,248); line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> ?????????productService.addEventListener(ResultEvent.RESULT,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> ??????????var?id?:?String??=?this.grid1.selectedItem["id"];??
  89. ?????????params?=?{"method":?"remove",?"id":?id};??
  90. ?????????productService.cancel();??
  91. ?????????productService.send(params);??
  92. ?*?清除form中的属性值??
  93. private?function?clearInputFields():void??
  94. ????productName.text?=?"";??
  95. ????remark.text?=?"";??
  96. }??

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读