Flex-Jsp 中Flex和Jsp传递中文参数问题的解决方法!(Tomcat服务器)

情况:

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 interpret external text files. The default value of System.useCodepage is false.
 


 


  •      
  • When the property is set to false, flash Player interprets 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 interprets 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 >
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值