本文共 3476 字,大约阅读时间需要 11 分钟。
初学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 >