Struts中文亂碼問題

 

在進行struts開發的過程中,總也是出現很多的亂碼問題 ,但歸根到底,也只是以下三種情況:

㈠頁面顯示中文亂碼

㈡傳遞參數中文亂碼

㈢國際化資源文件亂碼

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

㈠頁面顯示中文亂碼

      對於在頁面中顯示出現亂碼,這個問題比較簡單,便是檢查你的JSP文件裏是不是出現了中文要處理,因爲JSP默認的編碼格式爲“ISO-8859-1”,當JSP中出現要處理的中文時,其顯示就出現亂碼了,這種情況一般出現在手寫JSP,或修改時。因爲在myeclipse6.0中,如果出現了編碼錯誤時,程序不會讓你保存,而是會提示你注意編碼,這點很好。具體的修改辦法是把
Html代碼
1. <%.@ page language="java" import="java.util." pageEncoding="ISO-8859-1">  
<%.@ page language="java" import="java.util." pageEncoding="ISO-8859-1">

改成:
Html代碼
1. <%.@ page language="java" import="java.util." pageEncoding="GBK">   
 <%.@ page language="java" import="java.util." pageEncoding="UTF-8">


㈡傳遞參數中文亂碼

         傳遞參數出現的亂碼,參數的內容爲中文。比如在struts應用中,簡單的一個登錄界面中,需要傳遞的登錄名爲中文時,你沒經處理之前,是會出現亂碼傳遞的,爲了讓我們能看到顯示的亂碼,我們在對應的Action類的子類裏,修改一下,用System.out把接受到的參數輸出,代碼如下:
Java代碼
1. public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,  
2.         HttpServletResponse response) ...{  
3.     DynaActionForm loginForm = (DynaActionForm) form;  
4.   
5.     String username = (String) loginForm.get("username");  
6.     String password = (String) loginForm.get("password");  
7.     System.out.println("username:"+username);  
8.     System.out.println("password:"+password);  
9.     if (username.equals("ivorytower") && password.equals("123456")) ...{  
10.           return mapping.findForward("success");  
11.       }  
12.       return mapping.findForward("fail");  
13.       }  
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");
    }


那麼當你提交了中文輸入後就會出現亂碼了。

具體的解決方法有以下三種:

①修改Tomcat---->conf----->server.xml文件,在修改端口的標籤後面加一行代碼,如下:
Xml代碼
1. <Connector port="8080" protocol="HTTP/1.1"   
2.               connectionTimeout="20000"   
3.               redirectPort="8443"  URIEncoding="GBK"/>  
 <Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" URIEncoding="UTF-8"/>
此種方法只適用於正確處理GET方法提交的表單數據。這一點需要注意。自己費了好大的勁才發現。
②編寫過濾器Filter
Java代碼
1.       
2. import java.io.IOException;  
3.   
4. import javax.servlet.Filter;  
5. import javax.servlet.FilterChain;  
6. import javax.servlet.FilterConfig;  
7. import javax.servlet.ServletException;  
8. import javax.servlet.ServletRequest;  
9. import javax.servlet.ServletResponse;  
10.     
11.   public class CharacterEncodingFilter implements Filter ...{  
12.     
13.       @Override  
14.       public void destroy() ...{  
15.       }  
16.     
17.       @Override  
18.       public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException,ServletException   {  
19.       request.setCharacterEncoding("utf-8");  
20.       chain.doFilter(request, response);  
21.       }  
22.     
23.       @Override  
24.       public void init(FilterConfig arg0) throws ServletException ...{  
25.       }  
26.     
27.   }  
   
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傳遞的中文參數都設成“UTF-8”編碼。

③修改web.xml文件

    打開項目裏的web.xml文件,在前面加上如下代碼:
Xml代碼
1.      
2. <filter>  
3. <filter-name>characterEncoding</filter-name>  
4. <filter-class>com.v512.example.CharacterEncodingFilter</filter-class>  
5. </filter>  
6. <filter-mapping>  
7. <filter-name>characterEncoding</filter-name>  
8. <url-pattern>/*</url-pattern>  
9. </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代碼
1.       
2. <bean:message key="example.login.username"/>  
   
<bean:message key="example.login.username"/>

  再者,打開項目下的資源配置文件ApplicationResources.properties,依據上面所寫key值,設定成我們要的默認值(顯示英文),比如
引用
     
#Resource for Parameter 'com.v512.example.struts.ApplicationResources
#Project webexample2
example.login.username=username
example.login.password=password

現在我們動手新建一個資源文件,讓其能顯示中文,直接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文件,我們發現亂碼問題沒有了。

②利用Eclipse ResourceBundle Editor插件工具

      以上我們是利用了JDK的native2ascii工具來處理國際化問題,但在EC中,還有一種更方便的工具專門用來處理編輯java的資源文件國際化亂碼問題,即Eclipse ResourceBundle Editor插件工具。安裝了這個插件後,我們能進行方便的可視化資源文件編輯。推薦。
1. de.guhsoft.jinto-0.12.0.zip 這個附件在 jory.ys168.com上有
2.下載這個插件 ,解壓出來得到2個文件夾   features 和 plugins
3. 把這2個文件夾copy到 eclipse目錄下,D:\Program Files\MyEclipse 6.0\eclipse    這個是我的目錄
4,打開myeclipse -》help-》software-Updates-》find and installs -> Search for new features to install
-> New Remote Site   
   在彈出的窗口  
name:Properties Editor
選中 “Properties Editor ”     然後確認安裝就可以了
   裝完以後就可以 讀取 .properties的中文文件了
MyEclipse8.5的安裝不同,可以按照如下方式:
1.添加插件將features和plugins中的內容分別複製到C:/Genuitec/Common/features和C:/Genuitec/Common/plugins中
2.修改bundles.info
   在C:/Genuitec/MyEclipse8/configuration/org.eclipse.equinox.simpleconfigurator下有個bundles.info文件,編輯該文件:
jp.gr.java_conf.ussiy.app.propedit,5.3.3,file:/C:/Program Files/Genuitec/Common/plugins/jp.gr.java_conf.ussiy.app.propedit_5.3.3.jar,4,false
格式說明:包名,版本號,文件路經,4,false

Eclipse下的propedit5.3.3插件的安裝(沒有測試):
添加插件將features和plugins中的內容分別複製到Eclipse安裝目錄對應的features和plugins就好了。
在MyEclipse或者Eclipse項目下,右鍵點擊properties文件,採用open with用propertiesEditor打開就行了。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章