flex(通過URLLoader)與後台jsp進行互動的例子,包括中文亂碼的處理

來源:互聯網
上載者:User

初學flex,寫個flex(通過URLLoader)與後台互動的例子,解決中午亂碼的處理:

這是flex.jsp的代碼:

<%...@ page contentType="text/html; charset=UTF-8" %>
<%...
    String usr = java.net.URLDecoder.decode(request.getParameter("user"), "UTF-8");
    System.out.println("取得傳入的參數:"+usr);
    String psw = java.net.URLDecoder.decode(request.getParameter("psw"), "UTF-8");
    System.out.println("取得傳入的參數:"+psw);
%>
<%=usr%>+<%=psw%>

 

這是login.mxml的代碼:

 

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" 
    layout="absolute" fontSize="12" xmlns:local="*">
    <mx:TraceTarget/>
    <mx:Style>
        .lab{
           fontWeight: "bold";
           /*color: #FF0000;*/
           fontSize: 15;
        }
    </mx:Style>
    <mx:Script>
        <![CDATA[
        //對提交給背景參數進行UTF-8的編碼處理
        private function httpEncoding(param:String):String{
            return encodeURIComponent(param);
        }
        private function doLogin():void {
            //trace("focusEnabled:"+loading.focusEnabled);
            //this.focusManager.setFocus(user);
            var url:String = "http://localhost:8600/flex.jsp";
            var params:URLVariables = new URLVariables();
            //這個user,psw就是傳入背景參數user,jsp就用 request.getParameter("user")來取
            params.user = httpEncoding(user.text);
            params.psw = psw.text;
            var loader:URLLoader = new URLLoader();
            this.configureEventListeners(loader);
            //可以不設定,因為預設是text
            loader.dataFormat = URLLoaderDataFormat.TEXT;
            var request:URLRequest = new URLRequest(url);
            request.data = params;
            try{
                loader.load(request);
            }catch(error:Error){
                trace(error.message);
            }
        }
        private function configureEventListeners(dispatcher:IEventDispatcher):void {
            dispatcher.addEventListener(Event.COMPLETE, completeHandler);
            dispatcher.addEventListener(Event.OPEN, openHandler);
            dispatcher.addEventListener(ProgressEvent.PROGRESS, progressHandler);
            dispatcher.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
            dispatcher.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);
            dispatcher.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
        }
       private function completeHandler(event:Event):void {
            var loader:URLLoader = URLLoader(event.target);
            trace("--complete..."+event.target.data);
            //var dataXML:XML = XML(event.target.data);
            //trace(dataXML.toXMLString());
            btn_btn.enabled=true;
        }

        private function openHandler(event:Event):void {
            trace("openHandler: " + event);
            //this.focusManager.setFocus(loading);
            btn_btn.enabled=false;
        }

        private function progressHandler(event:ProgressEvent):void {
            trace("progressHandler loaded:" + event.bytesLoaded + " total: " + event.bytesTotal);
        }

        private function securityErrorHandler(event:SecurityErrorEvent):void {
            trace("securityErrorHandler: " + event);
        }

        private function httpStatusHandler(event:HTTPStatusEvent):void {
            trace("httpStatusHandler: " + event);
        }

        private function ioErrorHandler(event:IOErrorEvent):void {
            trace("ioErrorHandler: " + event);
        }
        ]]>
    </mx:Script>
    
    <mx:Panel title="歡迎登入WAP管理系統:" width="307" height="189" layout="absolute" verticalAlign="top" horizontalCenter="-10.5" verticalCenter="-9">
        <mx:Label x="32" y="25" text="登入口令:" width="59"/>
        <mx:TextInput id="user" x="99" y="23" width="147"/>
        <mx:Label x="32" y="53" text="登入密碼:" width="59"/>
        <mx:TextInput id="psw" x="99" y="51" displayAsPassword="true" width="147"/>
        <mx:ControlBar alpha="1">
            <mx:Button id="btn_btn" x="58" y="92" label="確 定" click="this.doLogin();"/>
            <mx:Button x="162" y="92" label="取 消"/>
            <mx:Label x="0" y="129" text="Powered by Keren" textAlign="right"/>
        </mx:ControlBar>
    </mx:Panel>
</mx:Application>

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.