以我自己的了解,在進(jìn)行struts開發(fā)的過程中,總也是出現(xiàn)很多的亂碼問題 ,但歸根到底,也只是以下三種情況:
㈠頁面顯示中文亂碼
㈡?zhèn)鬟f參數(shù)中文亂碼
㈢國際化資源文件亂碼
下面就這三中情況介紹怎么在具體項目中處理這些亂碼問題。而對于整體的處理思想,是要統(tǒng)一編碼為 UTF-8.
㈠頁面顯示中文亂碼
對于在頁面中顯示出現(xiàn)亂碼,這個問題比較簡單,便是檢查你的JSP文件里是不是出現(xiàn)了中文要處理,因為JSP默認(rèn)的編碼格式為“ISO-8859-1”,當(dāng)JSP中出現(xiàn)要處理的中文時,其顯示就出現(xiàn)亂碼了,這種情況一般出現(xiàn)在手寫JSP,或修改時。因為在myeclipse6.0中,如果出現(xiàn)了編碼錯誤時,程序不會讓你保存,而是會提示你注意編碼,這點(diǎn)很好。具體的修改辦法是把
<%.@ page language="java" import="java.util." pageEncoding="ISO-8859-1">
改成:
<%.@ page language="java" import="java.util." pageEncoding="UTF-8">
㈡?zhèn)鬟f參數(shù)中文亂碼
傳遞參數(shù)出現(xiàn)的亂碼,參數(shù)的內(nèi)容為中文。比如在struts應(yīng)用中,簡單的一個登錄界面中,需要傳遞的登錄名為中文時,你沒經(jīng)處理之前,是會出現(xiàn)亂碼傳遞的,為了讓我們能看到顯示的亂碼,我們在對應(yīng)的Action類的子類里,修改一下,用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");
}
那么當(dāng)你提交了中文輸入后就會出現(xiàn)亂碼了。
具體的解決方法:
①修改Tomcat---->conf----->server.xml文件,在修改端口的標(biāo)簽后面加一行代碼,如下:
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" URIEncoding="UTF-8"/>
②編寫過濾器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 ...{
}
}
利用過濾器,把requst傳遞的中文參數(shù)都設(shè)成“UTF-8”編碼。
③修改web.xml文件
打開項目里的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>
注意其過濾的URL為“/*”,表示當(dāng)前的request請求。為了使設(shè)置生效,重起tomcat。
㈢國際化資源文件亂碼
①利用JDK的native2ascii工具進(jìn)行編碼轉(zhuǎn)換
國際化問題,主要是為了處理文件在瀏覽器上的顯示問題,還是以登錄界面來說,比如在中文瀏覽器上,我們要看到中文顯示,對應(yīng)在英文瀏覽器上要顯示英文。那么我們在登錄那個界面處理上,就不能直接寫上我們的“用戶名”“密碼”等標(biāo)識了。就要用標(biāo)記轉(zhuǎn)換輸出了,修改為:
<bean:message key="example.login.username"/>
再者,打開項目下的資源配置文件ApplicationResources.properties,依據(jù)上面所寫key值,設(shè)定成我們要的默認(rèn)值(顯示英文),比如
#Resource for Parameter ''com.v512.example.struts.ApplicationResources
#Project webexample2
example.login.username=username
example.login.password=password
現(xiàn)在我們動手新建一個資源文件,讓其能顯示中文,直接Ctrl+C,Ctrl+V。改名為ApplicationResources_zh.properties,代碼如下:
#Resource for Parameter ''com.v512.example.struts.ApplicationResources
#Project webexample2
example.login.username=用戶名
example.login.password=密碼
但保存,myeclipse會報錯,這時我們需要修改資源文件的編碼格式。Windons---->Preferences---->Content Type------>Text----->JavaPropertiesFile,把其Default encoding改為“utf-8”,按“update”更新。這樣就能進(jìn)行保存了。但是當(dāng)我們進(jìn)行驗證會不是成功時,仍然給我們的是亂碼。
不急,我們還得做一項任務(wù),打開DOS窗口,CMD到資源文件所在目錄,運(yùn)用JDK的native2ascii工具把我們新建的資源文件改成另一個名字的資源文件,例如bank.properties。命令如下:
>native2ascii -encoding gbk ApplicationResources_zh.properties bank.properties
打開bank.properties資源文件,自動生成的代碼如下:
#Generated by ResourceBundle Editor (http://eclipse-rbe.sourceforge.net)
example.login.username = \u7528\u6237\u540D
example.login.password = \u5BC6\u7801
然后在myeclipse窗口中,把原來新建ApplicationResources_zh.properties 刪除,并把bank.properties改為ApplicationResources_zh.properties (為了方便記憶,管理)。然后重起tomcat或進(jìn)行reload文件,我們發(fā)現(xiàn)亂碼問題沒有了。
②利用Eclipse ResourceBundle Editor插件工具
以上我們是利用了JDK的native2ascii工具來處理國際化問題,但在EC中,還有一種更方便的工具專門用來處理編輯java的資源文件國際化亂碼問題,即Eclipse ResourceBundle Editor插件工具。安裝了這個插件后,我們能進(jìn)行方便的可視化資源文件編輯。推薦。。
文章出處:http://www.diybl.com/course/3_program/java/javashl/200845/108433.html