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

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