風(fēng)行天下

          JAVA太極
          posts - 4, comments - 10, trackbacks - 0, articles - 55
            BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

          ?????? 在做java開發(fā)的過程中一開始就碰到中文問題,剛接觸到j(luò)ava時用的編譯器是JCreate,這是個非常好用簡單的java編譯器,
          但他就在對中文的支持上有很大問題,當(dāng)時我就在想中國這么大的國家為什么在世界上總是比不過那些小國家,最嚴(yán)重的是有日文開發(fā)文檔
          就是沒有中文的,所以我們在開發(fā)java的時候總是會碰到中文問題。下面我就來說一下我在開發(fā)中是如何來處理中文的。
          ????? 首先講中文的字符的編碼,剛開始中文的編碼是GB2312后來發(fā)現(xiàn)他的常用字根本不能適應(yīng)現(xiàn)在的需求,所以就在他的基礎(chǔ)上增加了很多
          生僻字,不常用字得到了新的編碼GBK,我們一般來說都用GBK來實現(xiàn)我們的開發(fā),而如果說到國際化就不得不提Unicode (統(tǒng)一碼)顧名思
          義是一個將世界上各種文字統(tǒng)一在一起的東東。由美國各大電腦廠商組成的Unicode策進會來推動。目的,推廣一個世界通用的編碼體制,
          驚世界上所有常用的文字都涵蓋進去,從而減少個電腦商開發(fā)國外市場遇到的問題。
          ??????? 這些編碼系統(tǒng)也會互相沖突。也就是說,兩種編碼可能使用相同的數(shù)字代表兩個不同的字符,或使用不同的數(shù)字代表相同的字符。任何
          一臺特定的計算機(特別是服務(wù)器)都需要支持許多不同的編碼,但是,不論什么時候數(shù)據(jù)通過不同的編碼或平臺之間,那些數(shù)據(jù)總會有損壞
          的危險。
          ??? Unicode給每個字符提供了一個唯一的數(shù)字,不論是什么平臺,不論是什么程序,不論什么語言。Unicode標(biāo)準(zhǔn)已經(jīng)被這些工業(yè)界的領(lǐng)導(dǎo)
          們所采用,例如:Apple, HP, IBM, JustSystem, Microsoft, Oracle, SAP, Sun, Sybase, Unisys和其它許多公司。最新的標(biāo)準(zhǔn)都需要
          Unicode,例如XML, Java, ECMAScript (JavaScript), LDAP, CORBA 3.0, WML等等,并且,Unicode是實現(xiàn)ISO/IEC 10646的正規(guī)方式。許多
          操作系統(tǒng),所有最新的瀏覽器和許多其他產(chǎn)品都支持它。Unicode標(biāo)準(zhǔn)的出現(xiàn)和支持它工具的存在,是近來全球軟件技術(shù)最重要的發(fā)展趨勢。
          ?????? 而Unicode4.0后有了UTF (Unicode/UCS Transformation Format),Unicode推薦使用UTF-8和UTF-16兩種格式其中8和16指的是Bits數(shù)而不
          是Bytes數(shù)。
          UTF-16基本就是Unicode雙字節(jié)的實現(xiàn),加上一個應(yīng)付未來需要的擴充編碼機制(很少用)
          UTF-8 是一種不等幅的編碼方式,英數(shù)字(Ascii字碼)保持原狀,完全不受影響(因此不需要做轉(zhuǎn)換),而其他漢字資料須透過程序來轉(zhuǎn)換,
          會[變胖],因為每個字需要額外一個或兩個Bytes來編碼。
          ?????? 下面我們來看看實戰(zhàn)中我是如何做的:
          ??? 1.web.xml
          ?配置過濾器
          ?

          1 <!-- ?Filter?Configuration? -->
          2 ? < filter >
          3 ?? < filter - name > Set?Character?Encoding </ filter - name >
          4 ?? < filter - class > cn.redstoneinfo.commons.web.EncodingFilter </ filter - class > ??
          5 ? </ filter >
          6

          ????
          ??? 2.EncodingFilter.java:

          ?1 ? // 這里設(shè)置的默認(rèn)的編碼,可以在web.xml中設(shè)置,如果不設(shè)就用這里的
          ?2 ? protected ?String?encoding? = ? " UTF-8 " ;
          ?3
          ?4 ? protected ? boolean ?ignore? = ? true ;
          ?5
          ?6 ? /* ?
          ?7 ??*?@see?javax.servlet.Filter#init(javax.servlet.FilterConfig)
          ?8 ?? */

          ?9 ? public ? void ?init(FilterConfig?filterConfig)? throws ?ServletException? {
          10 ??String?paramValue? = ?filterConfig.getInitParameter( " encoding " );
          11 ?? if ?(paramValue? != ? null )? {
          12 ??? this .encoding? = ?paramValue;
          13 ??}

          14 ??String?value? = ?filterConfig.getInitParameter( " ignore " );
          15 ?? if ?(value? == ? null )
          16 ??? this .ignore? = ? true ;
          17 ?? else ? if ?(value.equalsIgnoreCase( " true " ))
          18 ??? this .ignore? = ? true ;
          19 ?? else ? if ?(value.equalsIgnoreCase( " yes " ))
          20 ??? this .ignore? = ? true ;
          21 ?? else
          22 ??? this .ignore? = ? false ;
          23 ?}

          24

          ?

          ??? 3.Struts-config.xml:
          ?設(shè)置資源文件的路徑
          ?

          1 < message - resources
          2 ??parameter = " cn.redstoneinfo.oss.security.web.security-resource " ?key = " SECURITY_RES " ? null = " false " />
          3 ??
          4 ? < message - resources
          5 ??parameter = " cn.redstoneinfo.oss.web.ApplicationResources " ? null = " false " />
          6

          ??? 4.role-list.jsp
          ?頁面編碼和調(diào)用資源
          ?

          ?1 <% @?page?contentType = " text/html;?charset=UTF-8 " ?language = " java " %> ?
          ?2 ????????
          ?3 ? < oss:panel?label = " security.role.list.title " ?bundle = " SECURITY_RES " >
          ?4 ? < table?width = " 100% " >
          ?5 ? < tr? class = " tr1 " ?align = " center " ?height = " 25 " >
          ?6 ????? < td?width = " 5% " >
          ?7 ????? </ td >
          ?8 ????? < td? > ??
          ?9 ?? < bean:message?key = " app.common.name " ? />
          10 ????? </ td >
          11 ????? < td? >
          12 ????? < bean:message?key = " app.common.activeDate " ? />
          13 ????? </ td >
          14 ????? < td >
          15 ????? < bean:message?key = " app.common.inactiveDate " ? />
          16 ????? </ td? >
          17 ????? < td >
          18 ????? < bean:message?key = " app.common.status " ? />
          19 ????? </ td > ???
          20 ????
          21 ? </ tr >
          22 ????????
          23 ???????? < input?type = " button " ?value = " ???<bean:message?key= " app.common. new " /> " ? class = " buttonnew " ?onclick = " javascript:document.forms[0].act.value='add';?goUrlFormTarget('role.do',?'roleForm',?'roleTreeFrame'); " > ????
          24

          ?
          ??? 5.ApplicationResources.properties
          ?

          1 app.common.name? = ?名字
          2 ?app.common.ip? = ?IP
          3 ?app.common.port? = ?端口
          4 ?app.common.status? = ?狀態(tài)?
          5

          ??? 6.build.xml
          ?編譯資源文件生成Unicode

          1 ? < native2ascii?src = " ${oss.src.dir} " ?encoding = " UTF-8 " ?dest = " ${target.dir}/WEB-INF/classes " >
          2 ??? < include?name = " **/*.properties " ? />
          3 ?? </ native2ascii > ??
          4

          ??? 7.轉(zhuǎn)換后的ApplicationResources.properties
          ?

          1 app.common.name? = ?\u540d\u5b57
          2 ?app.common.ip? = ?IP
          3 ?app.common.port? = ?\u7aef\u53e3
          4 ?app.common.status? = ?\u72b6\u6001
          5

          ??? 8.server.xml
          ?tomcat5.0以后post跟get提交方法不了不同的編碼處理,應(yīng)該用下面的方式來處理
          ????

          1 < Connector
          2 ??port = " 8080 " ???????????????maxHttpHeaderSize = " 8192 "
          3 ???????????????maxThreads = " 150 " ?minSpareThreads = " 25 " ?maxSpareThreads = " 75 "
          4 ???????????????enableLookups = " false " ?redirectPort = " 8443 " ?acceptCount = " 100 "
          5 ???????????????connectionTimeout = " 20000 " ?disableUploadTimeout = " true " ??URIEncoding = " UTF-8 " />
          6

          ??? 9.界面
          11.gif
          ??? 以上操作基本實現(xiàn)徹底消滅中文亂碼問題,當(dāng)然還有其他更好的辦法和應(yīng)用,我們這里只講struts的簡單應(yīng)用中如何避免相應(yīng)問題的處理辦法。


          評論

          # re: 完全解決java開發(fā)中的中文問題(側(cè)重struts)  回復(fù)  更多評論   

          2006-09-02 12:56 by CenturyMagus
          我轉(zhuǎn)載的這篇也可以解決struts中文問題的,我已經(jīng)試驗過了
          http://www.aygfsteel.com/fengtaishao/archive/2006/08/28/66124.html

          # re: 完全解決java開發(fā)中的中文問題(側(cè)重struts)  回復(fù)  更多評論   

          2006-09-02 13:00 by CenturyMagus
          發(fā)錯連接了,不好意思,呵呵
          http://blog.csdn.net/CenturyMagus/archive/2006/08/28/1131542.aspx

          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 井陉县| 高邮市| 铜山县| 页游| 方山县| 宁城县| 阳朔县| 深泽县| 晴隆县| 台北市| 四平市| 绥德县| 搜索| 南木林县| 双城市| 醴陵市| 兰考县| 沅陵县| 邵东县| 乐业县| 桓台县| 濮阳县| 武义县| 北票市| 海阳市| 侯马市| 巴青县| 伊金霍洛旗| 锡林郭勒盟| 噶尔县| 平陆县| 广安市| 安国市| 原阳县| 子洲县| 临沧市| 东宁县| 宁津县| 正阳县| 阳新县| 天台县|