Posted on 2008-06-18 17:13
七郎歸來 閱讀(257)
評論(0) 編輯 收藏
學了很久的java,接觸SSH也有一段時間了,寫成博文總是有點懶,最近在整理一些思緒,把SSH一點一滴放進博客里,以備以后改進,再學習。

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

㈠頁面顯示中文亂碼

㈡?zhèn)鬟f參數(shù)中文亂碼

㈢國際化資源文件亂碼

下面就這三中情況介紹怎么在具體項目中處理這些亂碼問題。而對于整體的處理思想,是要統(tǒng)一編碼為: UTF-8.(以myeclipse6支持的struts1.3為準)

㈠頁面顯示中文亂碼

對于在頁面中顯示出現(xiàn)亂碼,這個問題比較簡單,便是檢查你的JSP文件里是不是出現(xiàn)了中文要處理,因為JSP默認的編碼格式為“ISO-8859-1”,當JSP中出現(xiàn)要處理的中文時,其顯示就出現(xiàn)亂碼了,這種情況一般出現(xiàn)在手寫JSP,或修改時。因為在myeclipse6.0中,如果出現(xià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應用中,簡單的一個登錄界面中,需要傳遞的登錄名為中文時,你沒經(jīng)處理之前,是會出現(xiàn)亂碼傳遞的,為了讓我們能看到顯示的亂碼,我們在對應的Action類的子類里,修改一下,用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");
}



那么當你提交了中文輸入后就會出現(xiàn)亂碼了。

具體的解決方法:

①修改Tomcat---->conf----->server.xml文件,在修改端口的標簽后面加一行代碼,如下:

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"/>

②編寫過濾器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 
{
}
}



利用過濾器,把requst傳遞的中文參數(shù)都設成“UTF-8”編碼。

③修改web.xml文件

打開項目里的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>


注意其過濾的URL為“/*”,表示當前的request請求。為了使設置生效,重起tomcat。

㈢國際化資源文件亂碼

①利用JDK的native2ascii工具進行編碼轉換

國際化問題,主要是為了處理文件在瀏覽器上的顯示問題,還是以登錄界面來說,比如在中文瀏覽器上,我們要看到中文顯示,對應在英文瀏覽器上要顯示英文。那么我們在登錄那個界面處理上,就不能直接寫上我們的“用戶名”“密碼”等標識了。就要用標記轉換輸出了,修改為:

Html代碼
<bean:message key="example.login.username"/>
<bean:message key="example.login.username"/>

再者,打開項目下的資源配置文件ApplicationResources.properties,依據(jù)上面所寫key值,設定成我們要的默認值(顯示英文),比如

引用

#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”更新。這樣就能進行保存了。但是當我們進行驗證會不是成功時,仍然給我們的是亂碼。

不急,我們還得做一項任務,打開DOS窗口,CMD到資源文件所在目錄,運用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或進行reload文件,我們發(fā)現(xiàn)亂碼問題沒有了。

②利用Eclipse ResourceBundle Editor插件工具

以上我們是利用了JDK的native2ascii工具來處理國際化問題,但在EC中,還有一種更方便的工具專門用來處理編輯java的資源文件國際化亂碼問題,即Eclipse ResourceBundle Editor插件工具。安裝了這個插件后,我們能進行方便的可視化資源文件編輯。推薦。。