Posted on 2008-06-18 17:13
七郎歸來(lái) 閱讀(259)
評(píng)論(0) 編輯 收藏
學(xué)了很久的java,接觸SSH也有一段時(shí)間了,寫(xiě)成博文總是有點(diǎn)懶,最近在整理一些思緒,把SSH一點(diǎn)一滴放進(jìn)博客里,以備以后改進(jìn),再學(xué)習(xí)。

以我自己的了解,在進(jìn)行struts開(kāi)發(fā)的過(guò)程中,總也是出現(xiàn)很多的亂碼問(wèn)題 ,但歸根到底,也只是以下三種情況:

㈠頁(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)很好。具體的修改辦法是把


Html代碼
<%.@ page language="<SPAN class=hilite3><SPAN class=hilite3>java</SPAN></SPAN>" import="<SPAN class=hilite3><SPAN class=hilite3>java</SPAN></SPAN>.util." pageEncoding="ISO-8859-1">
<%.@ page language="java" import="java.util." pageEncoding="ISO-8859-1">


改成:

Html代碼
<%.@ page language="<SPAN class=hilite3><SPAN class=hilite3>java</SPAN></SPAN>" import="<SPAN class=hilite3><SPAN class=hilite3>java</SPAN></SPAN>.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ù)輸出,代碼如下:

Java代碼
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)簽后面加一行代碼,如下:

Xml代碼
<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

Java代碼
import <SPAN class=hilite3><SPAN class=hilite3>java</SPAN></SPAN>.io.IOException;
import <SPAN class=hilite3><SPAN class=hilite3>java</SPAN></SPAN>x.servlet.Filter;
import <SPAN class=hilite3><SPAN class=hilite3>java</SPAN></SPAN>x.servlet.FilterChain;
import <SPAN class=hilite3><SPAN class=hilite3>java</SPAN></SPAN>x.servlet.FilterConfig;
import <SPAN class=hilite3><SPAN class=hilite3>java</SPAN></SPAN>x.servlet.ServletException;
import <SPAN class=hilite3><SPAN class=hilite3>java</SPAN></SPAN>x.servlet.ServletRequest;
import <SPAN class=hilite3><SPAN class=hilite3>java</SPAN></SPAN>x.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文件,在前面加上如下代碼:

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ě)上我們的“用戶(hù)名”“密碼”等標(biāo)識(shí)了。就要用標(biāo)記轉(zhuǎn)換輸出了,修改為:

Html代碼
<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=用戶(hù)名
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)行方便的可視化資源文件編輯。推薦。。