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

??? 以上操作基本實現徹底消滅中文亂碼問題,當然還有其他更好的辦法和應用,我們這里只講struts的簡單應用中如何避免相應問題的處理辦法。