flex 乱码问题解决方法
flex与servlet交互数据,有以下几个方面需要注意
参数的传递:
中文乱码的解决
返回值的处理
以下是我今天写的一个执行正常的程序
flex代码如下:
encoding="utf-8"?>
xmlns:mx="http://www.adobe.com/2006/mxml"
layout="absolute">
import mx.rpc.events.ResultEvent;
import mx.controls.Alert;
private function doRequest():void{
this.btn_do.enabled=false;
var url:String="http://localhost:8080/Test/servlet/BUAA";
this.sevlet.url=url;
var param:URLVariables=new URLVariables();
//这个对象是用来设置url中需要传递的参数,将所传递的参数一一按照如下的格式设置,即可传递到后台服务器
//如下所示传递了两个参数,一个是username,一个是timestamp
param.username=this.txt.text;
param.timestamp=(new Date()).toString();
//
this.sevlet.send(param);//
}
//下面这个函数是服务器处理完毕后flex的处理函数,在下文的标签里有定义
private function resultHandler(event:ResultEvent):void{
Alert.show(event.result.user);
this.btn_do.enabled=true;
}
]]>
result="resultHandler(event)"/>
width="100%" height="90%">
click="doRequest();" fontSize="14"/>
fontSize="14"/>
Servlet端的代码如下
public void doGet(HttpServletRequest request, HttpServletResponse
response)
throws ServletException, IOException {
response.setContentType("text/xml;charset=utf-8");
String para = request.getParameter("username");
PrintWriter out = response.getWriter();
String strOut = new String(para.getBytes("ISO8859-1"),
"utf-8");
out.println("");
out.println(""+strOut+"");
out.println("");
out.flush();
out.close();
}
对于中文参数的传递,在flex里有以下两个对应的处理函数
//下面的这2个函数是用来处理flex中传递中文参数乱码问题的,一个编码,一个解码
private function htmlParaEncoding(para:String):String{
return encodeURIComponent(para);
}
private function htmlParaDecoding(para:String):String{
return decodeURIComponent(para);
}
不过一般在flex端只需要用编码函数,java中打印结果时使用java.net.URLDecoder.decode方法解码,如下面这个例子
flex代码:
encoding="utf-8"?>
xmlns:mx="http://www.adobe.com/2006/mxml"
layout="absolute" fontSize="12"
xmlns:local="*">
.lab{
fontWeight: "bold";
fontSize: 15;
}
//对提交给后台的参数进行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:8080/Test/servlet/BUAA";
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);
}
]]>
height="189" layout="absolute" verticalAlign="top"
horizontalCenter="-10.5" verticalCenter="-9">
width="59"/>
width="147"/>
width="59"/>
displayAsPassword="true" width="147"/>
click="this.doLogin();"/>
label="取消"/>
textAlign="right"/>
java代码
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);
out.flush();
out.close();
此外,在网上也参考到一些不错的文章,摘录如下:
http://www.cnblogs.com/dannyr/archive/2004/11/24/68026.html
原文
情况:
Flex默认使用的都是utf-8编码,包括Get,Post等方法。而Tomcat服务器端接收request对象默认是8859_1
编码,添加Tomcat的request Filter用request.setCharacterEncoding("utf-8");
来设置,这个方法属于Tomcat设置和Flex无关,暂不讨论!
flex ->Jsp:
有2种情况 //我试验第2种情况,测试通过
情况一、MXML源代码文件中写入的中文字符:
Flex使用 System.useCodepage =
true;即使用本地操作系统编码(GBK)设置Flex的处理编码。Jsp中用依然用ISO_8859_1 编码来处理,并转化为GBK
。这样Jsp可以正确解释Flex传递的中文字符。这个时候可以认为Flex对mxml源代码文件进行编译时候,源代码中的中文字符已经混乱了,所以要加上System.useCodepage
= true;语句, 按GBK编码将中文字符从Flex发送到Tomcat。
同时Tomcat中Jsp应该按GBK 重新编码
String categoryID = request.getParameter("categoryID");
String strOut = new String(categoryID.getBytes("ISO8859-1 "), "GBK
");
System.out.println("categoryID="+categoryID);
System.out.println("categoryID="+strOut);
情况二、Flex运行时候由输入框输入的中文字符
这个时候输入框输入的中文字符是一定为UTF-8编码的,所以Flex中System.useCodepage =
false;或者不设置,就默认utf-8编码格式传递数据,而Tomcat中Jsp使用下面语句按UTF-8来重新编码
String categoryID = request.getParameter("categoryID");
String strOut = new String(categoryID.getBytes("ISO8859-1"),
"utf-8");
System.out.println("categoryID="+categoryID);
System.out.println("categoryID="+strOut);
Jsp->Flex:
Jsp页面用页面指令
contentType="text/html;charset=utf-8"%>
设置,返回结果是utf-8编码,Flex接收后成功解释并正确显示。
原文:http://haitao-love-u.blog.sohu.com/116743431.html
通常办法在jsp页面中,加入
request.setCharacterEncoding("utf-8");
%>,flex默认编码是utf-8。然而,如果问题没有解决,依旧是乱码的话,那么,打开你的tomcat目录下的conf/server.xml文件,观察:
maxThreads="150" connectionTimeout="20000"
redirectPort="8443"
URIEncoding="UTF-8"/>,若没有URIEncoding="UTF-8",添加进去,这样问题就圆满解决了。顺便说下,mysql数据库编码也是utf-8
原文:http://java-007.javaeye.com/blog/381617
最近看一些文档,总结了一些给后台传递参数的方法,列举如下:
方法1:采用URLVariables对象
encoding="utf-8"?>
xmlns:mx="http://www.adobe.com/2006/mxml"
layout="absolute" fontSize="12"
>
import mx.controls.Alert;
import mx.rpc.events.ResultEvent;
//对提交给后台的参数进行UTF-8的编码处理
private function httpEncoding(param:String):String{
return encodeURIComponent(param);
}
private function httpEncoding0(param:String):String{
return param;//encodeURI(param);
}
private function doRequest():void{
btn_do.enabled=false;
var url:String = "http://localhost:8600/grid.jsp";
//以下那样写后台会乱码,不管是否做URI编码转换
//url += "?user="+httpEncoding0("用户名");
//url += "&psw="+httpEncoding0("密码");
//trace(url);
srv.url = url;
//srv.send();
//以下这样写正常
var params:URLVariables = new URLVariables();
//这个user,psw就是传入后台的参数user,jsp就用
request.getParameter("user")来取
params.user = httpEncoding("用户名");
params.psw = httpEncoding("密码");
srv.send(params);
}
private function resultHandler(event:ResultEvent):void{
Alert.show("与后台交互结束,前台开始取得的数据...","提示信息");
btn_do.enabled=true;
}
]]>
result="resultHandler(event);"/>
width="100%" height="90%">
click="doRequest();"/>
dataProvider="{srv.lastResult.catalog.product}" width="100%"
height="100%" y="28"/>
方法2:采用,同时也演示了mx:State的用法,[来自网上]
encoding="utf-8"?>
xmlns:mx="http://www.adobe.com/2006/mxml"
layout="absolute">
value="95%"/>
value="95%"/>
target="{password}"/>
target="{username}"/>
target="{label1}"/>
target="{Submit}"/>
target="{label2}"/>
value="Members Section"/>
position="lastChild">
position="lastChild">
position="lastChild">
text="Label"/>
import mx.rpc.events.ResultEvent;
]]>
private function checkLogin(evt:ResultEvent):void
{
if(evt.result.loginsuccess == "yes")
{
currentState = "Logged In";
}
if(evt.result.loginsuccess == "no")
{
mx.controls.Alert.show(''Invalid username/password'');
}
}
]]>
result="checkLogin(event)" showBusyCursor="true" method="POST"
url="http://www.vipercreations.com/site_admin/login.php"
useProxy="false">
{username.text}
{password.text}
height="200" layout="absolute" title="Login System"
horizontalCenter="0" verticalCenter="-2"
id="panel1">
id="label1"/>
id="username"/>
id="label2"/>
displayAsPassword="true"/>
click="login_user.send();"/>