zoukankan      html  css  js  c++  java
  • flex与java通信的小例子二

     适合初学者理解

    1,testflex.mxml

    1. <?xml version="1.0" encoding="utf-8"?>  
    2. <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"   
    3.                xmlns:s="library://ns.adobe.com/flex/spark"   
    4.                xmlns:mx="library://ns.adobe.com/flex/mx"  
    5.                creationComplete="init()"  
    6.                minWidth="955" minHeight="600">  
    7.     <fx:Declarations>  
    8.         <!-- 将非可视元素(例如服务、值对象)放 在此处 -->  
    9.     </fx:Declarations>  
    10.     <fx:Script>  
    11.         <!--[CDATA[  
    12.             import mx.collections.ArrayCollection;  
    13.             import mx.controls.Alert;  
    14.             import mx.rpc.events.FaultEvent;  
    15.             import mx.rpc.events.ResultEvent;  
    16.             import mx.rpc.http.HTTPService;  
    17.               
    18.             [Bindable]  
    19.             private var selectResponse:String= null;  
    20.             [Bindable]  
    21.             private var arraylist:ArrayCollection = new ArrayCollection([  
    22.                 {name:"CN",data:"中文"},  
    23.                 {name:"US",data:"英文"},  
    24.             ]);  
    25.             private function init():void{  
    26. //              fService.addEventListener(ResultEvent.RESULT,successhandler);  
    27. //              fService.addEventListener(FaultEvent.FAULT,faulthandler);  
    28.             }  
    29.             private function dropdownlistChangeHandler(event:Event = null):void{  
    30.                 var httpservice:HTTPService = new HTTPService();  
    31.                 httpservice.resultFormat = "text";  
    32.                 httpservice.url = "http://localhost:8080/testjava/flexServlet";  
    33.                 httpservice.method = "POST";  
    34.                 var param:Object = new Object;  
    35.                 param.selects = select.selectedItem.data;  
    36.                 httpservice.send(param);  
    37.                 httpservice.addEventListener(ResultEvent.RESULT,successhandler);  
    38. //              fService.sendRequest(select.selectedItem.data);  
    39. //              Alert.show(select.selectedItem.data);  
    40.             }  
    41.             private function successhandler(event:ResultEvent):void{  
    42.                 selectResponse = event.message.body.toString();  
    43.                 languages.text = selectResponse;  
    44. //              Alert.show(selectResponse);  
    45.             }  
    46. //          private function faulthandler(event:FaultEvent):void{  
    47. //              selectResponse = event.message.toString();  
    48. //              languages.text = selectResponse;  
    49. //          }  
    50.         ]]-->  
    51.     </fx:Script>  
    52.     <s:DropDownList x="109" y="66" id="select" labelField="name" dataProvider="{this.arraylist}" change="dropdownlistChangeHandler(event)">  
    53.     </s:DropDownList>  
    54.     <mx:Text id="languages" text="{selectResponse}" x="288" y="198"/>  
    55. </s:Application>  

    2,testjava.java

    1. package servlet;  
    2.   
    3. import java.io.IOException;  
    4. import java.io.PrintWriter;  
    5. import java.io.UnsupportedEncodingException;  
    6.   
    7. import javax.servlet.http.HttpServlet;  
    8. import javax.servlet.http.HttpServletRequest;  
    9. import javax.servlet.http.HttpServletResponse;  
    10.   
    11. public class FlexServlet extends HttpServlet {  
    12.     /** 
    13.      *  
    14.      */  
    15.     private static final long serialVersionUID = 1L;  
    16.   
    17.     public FlexServlet() {  
    18.         super();  
    19.     }  
    20.   
    21.     protected void doGet(HttpServletRequest request,  
    22.             HttpServletResponse response) {  
    23.         doPost(request, response);  
    24.     }  
    25.   
    26.     protected void doPost(HttpServletRequest request,  
    27.             HttpServletResponse response) {  
    28.         try {  
    29.             request.setCharacterEncoding("UTF-8");  
    30.         } catch (UnsupportedEncodingException e1) {  
    31.             // TODO Auto-generated catch block  
    32.             e1.printStackTrace();  
    33.         }  
    34.         String selects = request.getParameter("selects");  
    35.         response.setContentType("text/html;charset=UTF-8");  
    36.         PrintWriter pout = null;  
    37.         try {  
    38.             pout = response.getWriter();  
    39.         } catch (IOException e) {  
    40.             // TODO Auto-generated catch block  
    41.             e.printStackTrace();  
    42.         }  
    43.         pout.print("选择:" + selects);  
    44.     }  
    45. }  

    3,web.xml

    1. <?xml version="1.0" encoding="UTF-8"?>  
    2. <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"  
    3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    4.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   
    5.     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">  
    6.     <servlet>  
    7.         <servlet-name>flexServlet</servlet-name>  
    8.         <servlet-class>servlet.FlexServlet</servlet-class>  
    9.     </servlet>  
    10.     <servlet-mapping>  
    11.         <servlet-name>flexServlet</servlet-name>  
    12.         <url-pattern>/flexServlet</url-pattern>  
    13.     </servlet-mapping>  
    14.     <welcome-file-list>  
    15.         <welcome-file>testflex.html</welcome-file>  
    16.     </welcome-file-list>  
    17. </web-app>  

     源码下载地址

  • 相关阅读:
    JAVA 遍历UFT-8所有中文
    用友U8 | 【固定资产】固定资产常见问题
    用友U8 | 【出纳管理】当前凭证不是出纳管理凭证,不能修改
    用友U8 | 【出纳管理】选中的日记账包含了已制单但没有回写凭证号的日记账,无法制单!
    用友U8 | 【生产订单】工作日历未设定或超出工作日历范围
    用友U8 | 【出纳管理】当前选择的日记账已经制单,上次制单包含其他账户的日记账,请在日记账查询中修改
    用友U8 | 【U8年结】开账结转如何操作?
    用友U8 | 【出纳管理】收支操作生单提示:集合中的143272已经加锁了,不能对集合中的数据进行加锁
    用友U8 | 【出纳管理】银行记账提示:未设置对象变量或with block 变量(91)
    MySQL经典45题(二)
  • 原文地址:https://www.cnblogs.com/nianshi/p/1732449.html
Copyright © 2011-2022 走看看