有人說(shuō),亂碼問(wèn)題一直跟中國(guó)的程序員特別有緣,真是再同意不過(guò)了,不管是Struts,JSF,JSP,還是MySQL,Tomcat,全都或多或少有亂碼的問(wèn)題。
一般的做法有用Filter:
?< filter >
???? < filter-name > Set Character Encoding </ filter-name >
???? < filter-class > org.springframework.web.filter.CharacterEncodingFilter </ filter-class >
???? < init-param >
?????? < param-name > encoding </ param-name >
?????? < param-value > GBK </ param-value >
???? </ init-param >
???? < init-param >
?????? < param-name > ignore </ param-name >
?????? < param-value > true </ param-value >
???? </ init-param >
?? </ filter >
?? < filter-mapping >
???? < filter-name > Set Character Encoding </ filter-name >
???? < url-pattern > *.do </ url-pattern >
?? </ filter-mapping >
?? < filter-mapping >
???? < filter-name > Set Character Encoding </ filter-name >
???? < url-pattern > *.jsp </ url-pattern >
?? </ filter-mapping >
?? < filter-mapping >
???? < filter-name > Set Character Encoding </ filter-name >
???? < url-pattern > *.html </ url-pattern >
?? </ filter-mapping >
?? < filter-mapping >
???? < filter-name > Set Character Encoding </ filter-name >
???? < url-pattern > *.htm </ url-pattern >
?? </ filter-mapping >
的,有用
?<% request.setCharacterEncoding( " GBK " ); %>
的,還有用
?<% @ page contentType = " text/html; charset=GBK "? pageEncoding = " GBK " %>
<meta http-equiv="content-type" content="text/html; charset=GBK">
的,還可以用
?<%? String name? =?? new? String(request.getParameter( " name " ).getBytes( " 8859_1 " ),? " GB2312 " );? %>
昨天就在做項(xiàng)目的過(guò)程中,發(fā)現(xiàn)用URL傳request參數(shù)的時(shí)候,在第二個(gè)頁(yè)面上得到亂碼的問(wèn)題。把上面幾種方法都試了一下還是不行。仔細(xì)追蹤了一下,發(fā)現(xiàn)在頁(yè)面的源代碼上中文是正常的,一直到URL還是中文正常,可是在后臺(tái)的Action里面log出來(lái)就成了亂碼了,于是猜想是在request封裝的過(guò)程中把中文變成亂碼了,以致于后臺(tái)直接就是取到的亂碼。在后臺(tái)Action中Set入中文,頁(yè)面上正常顯示,說(shuō)明Struts的中文已經(jīng)不存在問(wèn)題。剩下的,應(yīng)該就只有doGet和doPost方法的問(wèn)題了。找了一下tomcat的配置文件,發(fā)現(xiàn)只要在server.xml中:
????? <!--? Define a non-SSL HTTP/1.1 Connector on port 8080? -->
???? < Connector? port ="8080"? maxHttpHeaderSize ="8192"
?????????????? maxThreads ="150"? minSpareThreads ="25"? maxSpareThreads ="75"
?????????????? enableLookups ="false"? redirectPort ="8443"? acceptCount ="100"
?????????????? connectionTimeout ="20000"? disableUploadTimeout ="true" />
???? <!--? Note : To disable connection timeouts, set connectionTimeout value
???? to 0? -->
改為
????? <!--? Define a non-SSL HTTP/1.1 Connector on port 8080? -->
???? < Connector? port ="8080"? maxHttpHeaderSize ="8192"
?????????????? maxThreads ="150"? minSpareThreads ="25"? maxSpareThreads ="75"
?????????????? enableLookups ="false"? redirectPort ="8443"? acceptCount ="100"
?????????????? connectionTimeout ="20000"? disableUploadTimeout ="true"? URIEncoding ="GBK"/>
???? <!--? Note : To disable connection timeouts, set connectionTimeout value
???? to 0? -->
就是加上URIEncoding="GBK"就萬(wàn)事大吉了。
再加上一條:
<session-factory>
<property name="hibernate.connection.url">jdbc:jtds:sqlserver://192.168.0.3:1433;DatabaseName=HomeConsume;charset=GBK</property>
<property name="hibernate.cglib.use_reflection_optimizer">true</property>
<property name="hibernate.connection.password">sju</property>
<property name="hibernate.connection.username">sa</property>
<property name="hibernate.connection.driver_class">net.sourceforge.jtds.jdbc.Driver</property>
<property name="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</property>
<mapping resource="net/magicyang/homeconsume/pojo/Test.hbm.xml" />
<mapping resource="net/magicyang/homeconsume/pojo/Consumeinfo.hbm.xml" />
<mapping resource="net/magicyang/homeconsume/pojo/Consumetype.hbm.xml" />
</session-factory>
至此,應(yīng)該再困難的亂碼問(wèn)題都解決了吧。就是要在頁(yè)面上、數(shù)據(jù)庫(kù)中、request里、doGet、doPost方法里面都是中文!看你還有什么地方躲??