初學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>