Flex4_HttpService組件


1、在JavaWeb項目中新建Servlet(FlexLoginServelt) :

public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        response.setContentType("text/html");
        response.setContentType("text/xml;charset=utf-8");
        PrintWriter out = response.getWriter();
        //接收Flex端傳來的參數
        String userName=request.getParameter("userName");
        String userPwd=request.getParameter("userPwd");
        String result="登錄失敗,用戶名或密碼錯誤!";
        if("admin".equals(userName) || "".equals(userPwd)){
            result="登錄成功!";
        }
        //將登錄信息返回給客戶端
        out.println(result);
        out.flush();
        out.close();
    }

2、Flex端代碼

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
               xmlns:s="library://ns.adobe.com/flex/spark" 
               xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600"
               initialize="init()">
    <fx:Script>
        <![CDATA[
            import mx.controls.Alert;
            import mx.rpc.events.FaultEvent;
            import mx.rpc.events.ResultEvent;
            
            private function init():void{
                btn_login.addEventListener(MouseEvent.CLICK,login);
                
                //加載HTTPService的返回監聽
                httpSer.addEventListener(ResultEvent.RESULT,httpSerResultHandler);
                httpSer.addEventListener(FaultEvent.FAULT,httpSerFaultHandler);
            }
            
            private function login(event:MouseEvent):void{
                httpSer.send();
            }
            
            //返回成功事件
            private function httpSerResultHandler(event:ResultEvent):void{
                Alert.show(event.result.toString(),"登錄提示");
            }
            //返回失敗事件
            private function httpSerFaultHandler(event:FaultEvent):void{
                Alert.show(event.fault.message as String,"登錄提示");
            }
            
        ]]>
    </fx:Script>
    <fx:Declarations>
        <s:HTTPService id="httpSer" url="http://localhost:8090/FlexHttpService/FlexLoginServelt" method="POST">
            <s:request>
                <!--需要發送到服務器的參數名,及值,接收參數名時必須一致-->
                <userName>{txt_userName.text}</userName>
                <userPwd>{txt_userPwd.text}</userPwd>
            </s:request>
        </s:HTTPService>
    </fx:Declarations>
    <s:Panel x="37" y="40" width="250" height="200">
        <s:Label x="37" y="28" text="用戶名:"/>
        <s:Label x="37" y="61" text="密    碼:"/>
        <s:TextInput id="txt_userName" x="82" y="24"/>
        <s:TextInput id="txt_userPwd" x="83" y="56" displayAsPassword="true"/>
        <s:Button x="83" y="115" label="登錄" id="btn_login"/>
    </s:Panel>
</s:Application>

3、效果圖:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM