㈠頁(yè)面顯示中文亂碼
㈡?zhèn)鬟f參數(shù)中文亂碼
㈢國(guó)際化資源文件亂碼
下面就這三中情況介紹怎么在具體項(xiàng)目中處理這些亂碼問(wèn)題。而對(duì)于整體的處理思想,是要統(tǒng)一編碼為: UTF-8.(以myeclipse6支持的struts1.3為準(zhǔn))
㈠頁(yè)面顯示中文亂碼
????? 對(duì)于在頁(yè)面中顯示出現(xiàn)亂碼,這個(gè)問(wèn)題比較簡(jiǎn)單,便是檢查你的JSP文件里是不是出現(xiàn)了中文要處理,因?yàn)镴SP默認(rèn)的編碼格式為“ISO-8859-1”,當(dāng)JSP中出現(xiàn)要處理的中文時(shí),其顯示就出現(xiàn)亂碼了,這種情況一般出現(xiàn)在手寫(xiě)JSP,或修改時(shí)。因?yàn)樵趍yeclipse6.0中,如果出現(xiàn)了編碼錯(cuò)誤時(shí),程序不會(huì)讓你保存,而是會(huì)提示你注意編碼,這點(diǎn)很好。具體的修改辦法是把
- < %.@?page? language = "java" ? import = "java.util." ? pageEncoding = "ISO-8859-1" > ??
<%.@ page language="java" import="java.util." pageEncoding="ISO-8859-1">
改成:
- < %.@?page? language = "java" ? import = "java.util." ? pageEncoding = "UTF-8" > ???
<%.@ page language="java" import="java.util." pageEncoding="UTF-8">
㈡?zhèn)鬟f參數(shù)中文亂碼
???????? 傳遞參數(shù)出現(xiàn)的亂碼,參數(shù)的內(nèi)容為中文。比如在struts應(yīng)用中,簡(jiǎn)單的一個(gè)登錄界面中,需要傳遞的登錄名為中文時(shí),你沒(méi)經(jīng)處理之前,是會(huì)出現(xiàn)亂碼傳遞的,為了讓我們能看到顯示的亂碼,我們?cè)趯?duì)應(yīng)的Action類(lèi)的子類(lèi)里,修改一下,用System.out把接受到的參數(shù)輸出,代碼如下:
- public ?ActionForward?execute(ActionMapping?mapping,?ActionForm?form,?HttpServletRequest?request, ??
- ????????HttpServletResponse?response)?...{ ??
- ????DynaActionForm?loginForm?=?(DynaActionForm)?form; ??
- ??
- ????String?username?=?(String)?loginForm.get( "username" ); ??
- ????String?password?=?(String)?loginForm.get( "password" ); ??
- ????System.out.println( "username:" +username); ??
- ????System.out.println( "password:" +password); ??
- ???? if ?(username.equals( "ivorytower" )?&&?password.equals( "123456" ))?...{ ??
- ???????? return ?mapping.findForward( "success" ); ??
- ????} ??
- ???? return ?mapping.findForward( "fail" ); ??
- ????}??
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) ...{ DynaActionForm loginForm = (DynaActionForm) form; String username = (String) loginForm.get("username"); String password = (String) loginForm.get("password"); System.out.println("username:"+username); System.out.println("password:"+password); if (username.equals("ivorytower") && password.equals("123456")) ...{ return mapping.findForward("success"); } return mapping.findForward("fail"); }
那么當(dāng)你提交了中文輸入后就會(huì)出現(xiàn)亂碼了。
具體的解決方法:
①修改Tomcat---->conf----->server.xml文件,在修改端口的標(biāo)簽后面加一行代碼,如下:
- < Connector ? port = "8080" ? protocol = "HTTP/1.1" ? ??
- ?????????????? connectionTimeout = "20000" ? ??
- ?????????????? redirectPort = "8443" ?? URIEncoding = "UTF-8" /> ??
<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" URIEncoding="UTF-8"/>
②編寫(xiě)過(guò)濾器Filter
- ???? ??
- import ?java.io.IOException; ??
- ??
- import ?javax.servlet.Filter; ??
- import ?javax.servlet.FilterChain; ??
- import ?javax.servlet.FilterConfig; ??
- import ?javax.servlet.ServletException; ??
- import ?javax.servlet.ServletRequest; ??
- import ?javax.servlet.ServletResponse; ??
- ??
- public ? class ?CharacterEncodingFilter? implements ?Filter?...{ ??
- ??
- ???? @Override ??
- ???? public ? void ?destroy()?...{ ??
- ????} ??
- ??
- ???? @Override ??
- ???? public ? void ?doFilter(ServletRequest?request,?ServletResponse?response,?FilterChain?chain)? throws ?IOException,ServletException???{ ??
- ????request.setCharacterEncoding( "utf-8" ); ??
- ????chain.doFilter(request,?response); ??
- ????} ??
- ??
- ???? @Override ??
- ???? public ? void ?init(FilterConfig?arg0)? throws ?ServletException?...{ ??
- ????} ??
- ??
- }??
import java.io.IOException; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; public class CharacterEncodingFilter implements Filter ...{ @Override public void destroy() ...{ } @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException,ServletException { request.setCharacterEncoding("utf-8"); chain.doFilter(request, response); } @Override public void init(FilterConfig arg0) throws ServletException ...{ } }
? 利用過(guò)濾器,把requst傳遞的中文參數(shù)都設(shè)成“UTF-8”編碼。
③修改web.xml文件
??? 打開(kāi)項(xiàng)目里的web.xml文件,在前面加上如下代碼:
- ??? ??
- < filter > ??
- < filter-name > characterEncoding </ filter-name > ??
- < filter-class > com.v512.example.CharacterEncodingFilter </ filter-class > ??
- </ filter > ??
- < filter-mapping > ??
- < filter-name > characterEncoding </ filter-name > ??
- < url-pattern > /* </ url-pattern > ??
- </ filter-mapping > ??
<filter> <filter-name>characterEncoding</filter-name> <filter-class>com.v512.example.CharacterEncodingFilter</filter-class> </filter> <filter-mapping> <filter-name>characterEncoding</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
注意其過(guò)濾的URL為“/*”,表示當(dāng)前的request請(qǐng)求。為了使設(shè)置生效,重起tomcat。
㈢國(guó)際化資源文件亂碼
???? ①利用JDK的native2ascii工具進(jìn)行編碼轉(zhuǎn)換
國(guó)際化問(wèn)題,主要是為了處理文件在瀏覽器上的顯示問(wèn)題,還是以登錄界面來(lái)說(shuō),比如在中文瀏覽器上,我們要看到中文顯示,對(duì)應(yīng)在英文瀏覽器上要顯示英文。那么我們?cè)诘卿浤莻€(gè)界面處理上,就不能直接寫(xiě)上我們的“用戶名”“密碼”等標(biāo)識(shí)了。就要用標(biāo)記轉(zhuǎn)換輸出了,修改為:
- ???? ??
- < bean:message ? key = "example.login.username" /> ??
<bean:message key="example.login.username"/>
? 再者,打開(kāi)項(xiàng)目下的資源配置文件ApplicationResources.properties,依據(jù)上面所寫(xiě)key值,設(shè)定成我們要的默認(rèn)值(顯示英文),比如
#Resource for Parameter 'com.v512.example.struts.ApplicationResources
#Project webexample2
example.login.username=username
example.login.password=password
現(xiàn)在我們動(dòng)手新建一個(gè)資源文件,讓其能顯示中文,直接Ctrl+C,Ctrl+V。改名為ApplicationResources_zh.properties,代碼如下:
#Resource for Parameter 'com.v512.example.struts.ApplicationResources
#Project webexample2
example.login.username=用戶名
example.login.password=密碼
但保存,myeclipse會(huì)報(bào)錯(cuò),這時(shí)我們需要修改資源文件的編碼格式。Windons---->Preferences---->Content Type------>Text----->JavaPropertiesFile,把其Default encoding改為“utf-8”,按“update”更新。這樣就能進(jìn)行保存了。但是當(dāng)我們進(jìn)行驗(yàn)證會(huì)不是成功時(shí),仍然給我們的是亂碼。
不急,我們還得做一項(xiàng)任務(wù),打開(kāi)DOS窗口,CMD到資源文件所在目錄,運(yùn)用JDK的native2ascii工具把我們新建的資源文件改成另一個(gè)名字的資源文件,例如bank.properties。命令如下:
>native2ascii -encoding gbk ApplicationResources_zh.properties bank.properties
打開(kāi)bank.properties資源文件,自動(dòng)生成的代碼如下:
#Generated by ResourceBundle Editor (http://eclipse-rbe.sourceforge.net)
example.login.username = \u7528\u6237\u540D
example.login.password = \u5BC6\u7801
然后在myeclipse窗口中,把原來(lái)新建ApplicationResources_zh.properties 刪除,并把bank.properties改為ApplicationResources_zh.properties (為了方便記憶,管理)。然后重起tomcat或進(jìn)行reload文件,我們發(fā)現(xiàn)亂碼問(wèn)題沒(méi)有了。
②利用Eclipse ResourceBundle Editor插件工具
????? 以上我們是利用了JDK的native2ascii工具來(lái)處理國(guó)際化問(wèn)題,但在EC中,還有一種更方便的工具專(zhuān)門(mén)用來(lái)處理編輯java的資源文件國(guó)際化亂碼問(wèn)題,即Eclipse ResourceBundle Editor插件工具。安裝了這個(gè)插件后,我們能進(jìn)行方便的可視化資源文件編輯。推薦。。
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元
