情况: Flex默认使用的都是utf-8编码,包括Get,Post等方法。而Tomcat服务器 端接收request对象默认是8859_1 编 码,添加Tomcat的request Filter用request.setCharacterEncoding("utf-8"); 来设置,这个方法属于Tomcat设置和 Flex无关,暂不讨论! flex ->Jsp: 有 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页面用页面指令<%@ page contentType="text/html;charset=utf-8"%> 设置,返回结果是utf-8编 码,Flex接收后成功解释并正确显示。 测试环境: Windows2000 Server (字符集为GBK) Tomcat 5.0.28 (默认设置) JDK1.5.0 flex 1.5 (默认设置) SqlServer2000 Sp3 测试代码: (仅仅为第二种情况,第一种情况酌情修改即可 ) 表结构 其中categoryid使用中 文内容
create
table
tblMobile (id
int
, name
varchar
(
20
), price
decimal
(
10
,
2
),
image
varchar
(
50
), categoryid
varchar
(
20
))
phonelist.jsp 这里数据库连接是SqlServer2000
<?
xml version
=
"
1.0
"
encoding
=
"
utf-8
"
?>
<%
@ page contentType
=
"
text/html;charset=utf-8
"
%>
<%
@ page
import
=
"
java.sql.*
"
%>
<
phonelist
>
<%
String sql
=
""
; String url
=
""
; String categoryID
=
request.getParameter(
"
categoryID
"
);
try
...
{ Class .Class.forName( " com.microsoft.jdbc.sqlserver.SQLServerDriver " ).newInstance(); url = " jdbc:microsoft:sqlserver://myserver:1433;DatabaseName=flex;User=flexassword=flex; " ; Connection conn = DriverManager.getConnection(url); Statement stmt = conn.createStatement(); String strOut = new String(categoryID.getBytes( " ISO8859-1 " ), " GBK " ); System.out.println( " categoryID= " + categoryID); System.out.println( " categoryID= " + strOut); sql = " select id, name, price, image from tblMobile where categoryid=' " + strOut + " ' " ; ResultSet rs = stmt.executeQuery(sql); while (rs.next()) ... { out.println( " <phone id=" " + rs.getString( 1 ) + " "> " ); out.println( " <id> " + rs.getString( 1 ) + " </id> " ); out.println( " <name> " + rs.getString( 2 ) + " </name> " ); out.println( " <price> " + rs.getString( 3 ) + " </price> " ); out.println( " <image> " + rs.getString( 4 ) + " </image> " ); out.println( " </phone> " ); } rs.close(); stmt.close(); conn.close(); }
catch
(Exception e)
...
{ out.println(e); }
%>
</
phonelist
>
test.mxml 其中HTTPService 使用自定义request 对象传递数据,注意前面的System.useCodepage = true; 语 句
<?
xml version="1.0" encoding="utf-8"
?>
<
mx:Application
xmlns:mx
="http://www.macromedia.com/2003/mxml"
verticalGap
="10"
backgroundColor
="#FFFFFF"
pageTitle
="手机"
initialize
="initApp()"
>
<
mx:HTTPService
id
="phoneService"
url
="phonelist.jsp"
fault
="alert(event.fault.faultstring)"
/>
<
mx:Model
id
="phonelist"
>
{phoneService.result.phonelist.phone}
</
mx:Model
>
<
mx:Script
>
<![CDATA[
var categoryId = 1; var categoryName = "Moto"; function initApp() { System.useCodepage = true; categoryId = "目录1"; var obj = new Object(); obj["categoryID"] = categoryId; phoneService.send(obj); }
]]>
</
mx:Script
>
<
mx:HBox
>
<
mxinkBar
styleName
="title"
width
="500"
click
=""
>
<
mx:dataProvider
>
<
mx:Array
>
<
mx:Object
label
="首 页"
link
="main"
/>
<
mx:Object
label
="手机分类"
link
="catagory"
/>
<
mx:Object
label
="论 坛"
link
="forum"
/>
<
mx:Object
label
="关 于"
link
="about"
/>
</
mx:Array
>
</
mx:dataProvider
>
</
mxinkBar
>
<
mxabel
text
="搜索"
/>
<
mx:TextInput
id
="key"
width
="120"
/>
<
mx:Button
label
="Go"
click
="initApp();"
/>
</
mx:HBox
>
<
mxataGrid
dataProvider
="{phonelist}"
>
<
mx:columns
>
<
mx:Array
>
<
mxataGridColumn
columnName
="id"
headerText
="ID"
/>
<
mxataGridColumn
columnName
="name"
headerText
="Name"
/>
<
mxataGridColumn
columnName
="image"
headerText
="Image"
/>
</
mx:Array
>
</
mx:columns
>
</
mxataGrid
>
<
mx:HBox
horizontalAlign
="center"
>
<
mxabel
text
="Copy Right 2004 dannyr's Studio "
/>
</
mx:HBox
>
</
mx:Application
>
这里有几个标 签有问题,因为网上看到的那篇文章里把:D这些字符替换成QQ表情了
结果: 在Jsp 页面里按8859_1 编码可以成功获取Flex传递的 中文内容。 备注: 这个方法是对Tomcat 的,其他的Java应用服务器的Request 处理方式可能不同,应区分对待! 引 用: 以下是Flex文档关于System.useCodepage 的说明:(比较简单,就不翻译了)
System.useCodepage
Availability
flash Player 6.
Usage
System.useCodepage
:Boolean
Description
Property; a Boolean value that tells flash Player whether to use Unicode or the traditional code page of the operating system running the player to interdivt external text files. The default value of System.useCodepage is false
.
When the property is set to false
, flash Player interdivts external text files as Unicode. (These files must be encoded as Unicode when you save them.) When the property is set to true
, flash Player interdivts external text files using the traditional code page of the operating system running the player.
总结下: MXML文件里一定要把下面的代码加上,否则改编 码,xml和mxml统一编码的方法都行不通
<
mx:Script
>
<![CDATA[
import flash.system.System; System.useCodePage = true;
]]>
</
mx:Script
>