关于上个Flex-Jsp-DB例子中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.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
        url 
= "jdbc:microsoft:sqlserver://myserver:1433;DatabaseName=flex;User=flex;Password=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>
        
<mx:LinkBar 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>
        
</mx:LinkBar>
        
<mx:Label text="搜索"/>
        
<mx:TextInput id="key" width="120"/>
        
<mx:Button label="Go" click="initApp();"/>
    
</mx:HBox>
    
    
<mx:DataGrid dataProvider="{phonelist}">
        
<mx:columns>
            
<mx:Array>
                
<mx:DataGridColumn columnName="id" headerText="ID"/>
                
<mx:DataGridColumn columnName="name"  headerText="Name"/>
                
<mx:DataGridColumn columnName="image"  headerText="Image"/>
            
</mx:Array>
        
</mx:columns>
    
</mx:DataGrid>

    
<mx:HBox horizontalAlign="center">
        
<mx:Label text="Copy Right 2004  dannyr's Studio "/>
    
</mx:HBox>
</mx:Application>

结果:
在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.
posted @ 2004-11-24 14:01 dannyr|一个都不能少! 阅读(5913) 评论(17)  编辑 收藏 网摘 所属分类: Flex技术

  回复  引用    
#1楼 2004-12-28 21:52 | dy [未注册用户]
程序中categoryId = "目录1";改成categoryId = key.text,不能传递中文到Jsp中.

  回复  引用  查看    
#2楼 [楼主]2004-12-29 08:54 | dannyr|一个都不能少!      
按文中的相同的运行环境配置,将程序中categoryId = "目录1";改成categoryId = key.text;是可以将中文传递到Jsp中的,我已经测试通过!
  回复  引用    
#3楼 2004-12-29 10:35 | dy [未注册用户]
奇怪!改成categoryId = key.text后,仅仅能传递 '目录1' ,传其他汉字如'中文'则出现乱码(categoryID=????????????
categoryID=锟斤拷锟斤拷)!

请帮忙再看一下.
  回复  引用  查看    
#4楼 [楼主]2004-12-29 16:25 | dannyr|一个都不能少!      
我修改了正文重新测试了一下,问题应该可以解决了!
  回复  引用    
#5楼 2004-12-30 14:45 | dy [未注册用户]
谢谢!果真如此 ,高手!

个人感觉Flex支持中文够 ,如果不用send(obj)方式,而用以下方式,得到乱码:
<mx:HTTPService id="srv" url="employeehttp.jsp" method="POST">
<mx:request>
<firstName>{firstName.text}</firstName>
</mx:request>
</mx:HTTPService>
  回复  引用    
#6楼 2005-06-29 14:29 | YuLimin [未注册用户]
感觉应当是这样来做吧:

categoryID = java.net.URLDecoder.decode(categoryID,"UTF-8");
  回复  引用    
#7楼 2005-09-18 13:56 | flyjava [未注册用户]
<mx:DataGrid dataProvider="{userSrv.result.users.user}" width="100%" height="224">
<mx:columns>
<mx:Array>
<mx:DataGridColumn columnName="name" headerText="User Name" />
<mx:DataGridColumn columnName="email" headerText="User Email"/>
</mx:Array>
</mx:columns>
</mx:DataGrid>

数据如上,显示出来就是用户列表了,但是如何才能在双击每行记录的时候,激发一个事件呢,谢谢
我的mail:flyjava@163.com
  回复  引用    
#8楼 2005-09-27 09:46 | 大鸟 [未注册用户]
"传其他汉字如'中文'则出现乱码(categoryID=????????????
categoryID=锟斤拷锟斤拷)"
这个问题到底怎么解决呀?!

  回复  引用  查看    
#9楼 [楼主]2005-09-29 17:08 | dannyr|一个都不能少!      
JSP里面,可以修改
String strOut = new String(categoryID.getBytes("ISO8859-
1"), "GBK");

中的字符集,比如UTF-8来看看效果,主要是Flex发送的字符也就是Flash客户段提交到服务器的字符集要和服务器获取数据的字符集一致就好了
  回复  引用    
#10楼 2005-10-10 12:20 | lengpeng [未注册用户]
我运行test.mxml时,页面跳出对话框显示
"java.lang.RuntimeException:You are not allowed to access the URL
http://localhost:8080/flex/flextest/phonelist.jsp?categoryID=key.text via this proxy. The

URL is not in the proxy's whitelist."
这是什么原因?麻烦大家了
  回复  引用  查看    
#11楼 [楼主]2005-10-12 15:02 | dannyr|一个都不能少!      
没配置过Flex的安全访问控制
配置文件:\WEB-INF\flex\flex-config.xml
<http-service-proxy>
<whitelist>
<!-- whitelist config for unnamed services -->
<unnamed>
<url>http://*</url><!-- 新添加条目 -->
</unnamed>
</whitelist>
</http-service-proxy>
  回复  引用    
#12楼 2006-07-05 11:54 | Yim [未注册用户]
学到了!
  回复  引用    
#13楼 2007-07-05 11:30 | yaoliang [未注册用户]
Severity and Description Path Resource Location Creation Time Id
1119: 访问可能未定义的属性 useCodepage (通过 static 类型 Class 引用)。 yy ll.mxml line 42 1183606108390 25

高手请问这是怎么回事呢????????????????????
我用的flex 2.0做的!!!!!!!!这是运行时报的错呢!!
  回复  引用    
#14楼 2007-07-05 12:21 | yaoliang [未注册用户]
你好!!这怎么布置上去呢!!mxml放那!jsp放那~~~!flex和tomcat有什么联系呢~~!
谢谢你给我回答!!
急~~~~~~~~~~~

  回复  引用    
#15楼 2007-07-05 15:38 | dannyr [未注册用户]
@yaoliang
很抱歉,我的这些例子是flex1.0时候的,现在2.0的语法、类库都不一样了,请自行查看SDK文档!
  回复  引用    
#16楼 2007-07-05 16:14 | dannyr [未注册用户]
@yaoliang
我的这些例子是flex1.0时候的,在1.0的时候flex的mxml需要在一个java服务器上运行,同时需要flex的运行库,mxml在服务器上动态编译执行,这里我使用了tomcat!当然也可以把mxml事先编译成swf文件,这样就不需要java服务器了,现在flex2.0一般都用后一种模式,直接编译成swf。
  回复  引用    
#17楼 2007-07-06 13:53 | yaoliang [未注册用户]
@dannyr
那你的代码能在Flex2运行吗???

请问现在的flex2怎么与数据库sql连接呢??
能给我个例子吗??
谢谢你了~~!!






标题  
姓名  
主页
Email (博主才能看到) 
验证码 *  看不清,换一张 [登录][注册]
内容(请不要发表任何与政治相关的内容)  
  登录  使用高级评论  新用户注册  返回页首  恢复上次提交      
该文被作者在 2004-12-29 16:27 编辑过
Google站内搜索


China-pub 计算机图书网上专卖店!6.5万品种 2-8折!
近千种 9-95 新二手计算图书火热销售中!

相关文章:

相关链接: