Flash+BlazeDS+Spring+MySQL 中文亂碼問題

來源:互聯網
上載者:User

在此環境下解決中文亂碼問題,從前台到資料庫,最好使用統一的UTF8編碼。這裡列出的個環節並不是都要設定,可以一步一步來查。如果是資料庫中有亂碼,先試一,如果是FLEX顯示有問題,看三、四。

一、MySQL亂碼

如果在資料庫中查看的中文也是亂碼,那先檢查MySQL的設定。

1、建立的表格使用UTF8字元集:

CREATE TABLE `table` (

  `Id` int(11) NOT NULL auto_increment,

  …..

  `createTime` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,

  PRIMARY KEY  (`Id`)

) ENGINE=InnoDB  DEFAULT CHARSET=UTF8;

2、my.ini設定預設字元集:

character-set-server=utf8

default-character-set=utf8

可用show variables like 'character%';來查看MySQL字元編碼相關的變數值。

二、Spring定義datasource

在這指的是定義在bean中的datasource的串連url。datasource定在在(/WEB-INF/spring/infrastructure-config.xml)

<bean id="mysqlDS" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/Web2Test?useUnicode=true&amp;characterEncoding=utf8"/>
        <property name="username" value="root" />
        <property name="password" value="admin123456" />
    </bean>

 

三、檔案的編碼

不管是java還是flex項目,代碼檔案格式都使用UTF8

在project>properties>Resource裡設定Text file encoding為utf-8

 

四、Flash端,設定flash.system.System.useCodePage=true;

“flash.system.System.useCodePage它決定使用哪個字碼頁來解釋外部文字檔。當此屬性設定為 false 時,會將外部文字檔解釋為 Unicode。(儲存這些檔案時,必須使用 Unicode 對其進行編碼。)當此屬性設定為
true 時,將使用運行該應用程式的作業系統的傳統字碼頁來解釋外部文字檔。useCodePage 的預設值是
false。”

 

五、Tomcat設定

/tomcat/conf/server.xml

<Connector port="8400" protocol="HTTP/1.1" connectionTimeout="20000" 
URIEncoding="UTF-8"
redirectPort="9400"/>

 

六、Java端

給WEB.XML添加一編碼轉換的過濾器。

<filter>

   <filter-name>Character Encoding Filter</filter-name>

       <filter-class>edu.lltang.characterEncodingFilter</filter-class>

          <init-param>

               <param-name>encoding</param-name>

               <param-value>UTF-8</param-value>

          </init-param>

      </filter>

<filter-mapping>

     <filter-name>Character Encoding Filter</filter-name>

     <url-pattern>/*</url-pattern>

</filter-mapping>

 

edu.lltang.characterEncodingFilter.java

public class characterEncodingFilter implements Filter {

   private FilterConfig filterConfig = null;

   public void destroy() {}

   @Override

   public void doFilter(ServletRequest arg0, ServletResponse arg1,FilterChain arg2) throws IOException, ServletException {

      arg0.setCharacterEncoding(filterConfig.getInitParameter("encoding"));

      arg2.doFilter(arg0, arg1);

   }

   @Override

      public void init(FilterConfig arg0) throws ServletException {

         this.filterConfig = arg0;

      }

}

 

七、XML

如果用到了XML來交換資料,記得在XML中顯示指定編碼。<?xml version="1.0" encoding="utf-8"?>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.