struts、spring、hibernate中文亂碼問題解決

近日,使用struts 1.1,發現討厭的中文亂碼問題,在form的傳送過程和入庫時候出現。就我在網絡上找的方法羅列如下:
(Tomcat 5.0.28+struts 1.1+hibernate 2.1+sqlserver2k)
1.直接轉編碼public static String isoToGB(String src){
String strRet=null;
try{
strRet = new String(src.getBytes("ISO_8859_1"),"GB2312");
}catch(Exception e) {
} return strRet;
}通過一個函數轉編碼,我沒有成功,不知爲何!

2.過濾filter設置法

package yourbean;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;

public class servfilter extends HttpServlet implements Filter { private FilterConfig filterConfig; //Handle the passed-in FilterConfig public void init(FilterConfig filterConfig) { this.filterConfig = filterConfig; } //Process the request/response pair public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) { try { request.setCharacterEncoding("GB2312"); ((HttpServletResponse)response).setHeader("Cache-control","no-cache"); response.setHeader("Pragma","No-cache"); response.setHeader("Cache-Control","no-cache"); response.setHeader("Expires","0"); ((HttpServletResponse)response).setHeader("Pragram","no-cache"); filterChain.doFilter(request, response); } catch(ServletException sx) { filterConfig.getServletContext().log(sx.getMessage()); } catch(IOException iox) { filterConfig.getServletContext().log(iox.getMessage()); } } //Clean up resources public void destroy() { }}下面是一個web.xml文件你用jbuilder寫上面的bean的時候會生成一個<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">

<web-app> <display-name>Welcome to Tomcat</display-name> <description> Welcome to Tomcat </description> <filter> <filter-name>servfilter</filter-name> <filter-class>yourbean.servfilter</filter-class> </filter> <filter-mapping> <filter-name>servfilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping></web-app>把上面的servfilter編譯放在你的web-inf /classes/yourbean/下web.xml放在web-inf/下和classes在一個目錄下在每個jsp頁面上加上<%@page contentType="text/html;charset=GBK"%>

也不是很方便,而且在tomcat也沒有成功,繼續鬱悶!

3.我現在使用方法,推薦!!

寫一個myActionServlet來並覆蓋ActionServlet中的process()方法。

protected void process(HttpServletRequest request, HttpServletResponse response) throws java.io.IOException, javax.servlet.ServletException { /**@todo Override this org.apache.struts.action.ActionServlet method*/ request.setCharacterEncoding("GB2312");//就加着一行一切都解決了 super.process(request, response); }

當然別忘了改一下web.xml裏面的配置 <servlet> <servlet-name>action</servlet-name> <servlet-class>strutsdemo.myActionServlet</servlet-class> <init-param> <param-name>debug</param-name> <param-value>2</param-value> </init-param> <init-param> <param-name>config</param-name> <param-value>/WEB-INF/struts-config.xml</param-value> </init-param> <load-on-startup>2</load-on-startup> </servlet>

改一下servlet-class標籤中的內容就可以!

真的可以,一勞用yi!

具體編碼的理論就不說了,google上已經夠多了。

另外,如果不用struts的話,hibernate也可能碰到中文亂碼問題,只要在hibernate.cfg.xml配置中如下:

<property name="hibernate.connection.url"> jdbc:microsoft:sqlserver://Localhost:1433;SelectMethod=cursor;characterEncoding=GBK;DatabaseName=myDatabase. </property>

characterEncoding=GBK!就可以了。


********************************************************************************************************************
Java/J2EE中文問題終極解決之道
Java中文問題一直困擾着很多初學者,如果瞭解了Java系統的中文問題原理,我們就可以對中文問題能夠採取根本的解決之道。

  最古老的解決方案是使用String的字節碼轉換,這種方案問題是不方便,我們需要破壞對象封裝性,進行字節碼轉換。

  還有一種方式是對J2EE容器進行編碼設置,如果J2EE應用系統脫離該容器,則會發生亂碼,而且指定容器配置不符合J2EE應用和容器分離的原則。

  在Java內部運算中,涉及到的所有字符串都會被轉化爲UTF-8編碼來進行運算。那麼,在被Java轉化之前,字符串是什麼樣的字符集? Java總是根據操作系統的默認編碼字符集來決定字符串的初始編碼,而且Java系統的輸入和輸出的都是採取操作系統的默認編碼。

  因此,如果能統一Java系統的輸入、輸出和操作系統3者的編碼字符集合,將能夠使Java系統正確處理和顯示漢字。這是處理Java系統漢字的一個原則,但是在實際項目中,能夠正確抓住和控制住Java系統的輸入和輸出部分是比較難的。J2EE中,由於涉及到外部瀏覽器和數據庫等,所以中文問題亂碼顯得非常突出。

  J2EE應用程序是運行在J2EE容器中。在這個系統中,輸入途徑有很多種:一種是通過頁面表單打包成請求(request)發往服務器的;第二種是通過數據庫讀入;還有第3種輸入比較複雜,JSP在第一次運行時總是被編譯成 Servlet,JSP中常常包含中文字符,那麼編譯使用javac時,Java將根據默認的操作系統編碼作爲初始編碼。除非特別指定,如在 Jbuilder/eclipse中可以指定默認的字符集。

  輸出途徑也有幾種:第一種是JSP頁面的輸出。由於JSP頁面已經被編譯成Servlet,那麼在輸出時,也將根據操作系統的默認編碼來選擇輸出編碼,除非指定輸出編碼方式;還有輸出途徑是數據庫,將字符串輸出到數據庫。

  由此看來,一個J2EE系統的輸入輸出是非常複雜,而且是動態變化的,而Java是跨平臺運行的,在實際編譯和運行中,都可能涉及到不同的操作系統,如果任由Java自由根據操作系統來決定輸入輸出的編碼字符集,這將不可控制地出現亂碼。

  正是由於Java的跨平臺特性,使得字符集問題必須由具體系統來統一解決,所以在一個Java應用系統中,解決中文亂碼的根本辦法是明確指定整個應用系統統一字符集。

  指定統一字符集時,到底是指定ISO8859_1 、GBK還是UTF-8呢?

  (1)如統一指定爲ISO8859_1,因爲目前大多數軟件都是西方人編制的,他們默認的字符集就是ISO8859_1,包括操作系統Linux和數據庫MySQL等。這樣,如果指定Jive統一編碼爲ISO8859_1,那麼就有下面3個環節必須把握:

  開發和編譯代碼時指定字符集爲ISO8859_1。

  運行操作系統的默認編碼必須是ISO8859_1,如Linux。

  在JSP頭部聲明:<%@ page contentType="text/html;charset=ISO8859_1" %>。

  (2)如果統一指定爲GBK中文字符集,上述3個環節同樣需要做到,不同的是隻能運行在默認編碼爲GBK的操作系統,如中文Windows。

  統一編碼爲ISO8859_1和GBK雖然帶來編制代碼的方便,但是各自只能在相應的操作系統上運行。但是也破壞了Java跨平臺運行的優越性,只在一定範圍內行得通。例如,爲了使得GBK編碼在linux上運行,設置Linux編碼爲GBK。

  那麼有沒有一種除了應用系統以外不需要進行任何附加設置的中文編碼根本解決方案呢?

  將Java/J2EE系統的統一編碼定義爲UTF-8。UTF-8編碼是一種兼容所有語言的編碼方式,惟一比較麻煩的就是要找到應用系統的所有出入口,然後使用UTF-8去“結紮”它。

  一個J2EE應用系統需要做下列幾步工作:

1. 開發和編譯代碼時指定字符集爲UTF-8。JBuilder和Eclipse都可以在項目屬性中設置。
2. 使用過濾器,如果所有請求都經過一個Servlet控制分配器,那麼使用Servlet的filter執行語句,將所有來自瀏覽器的請求(request)轉換爲UTF-8,因爲瀏覽器發過來的請求包根據瀏覽器所在的操作系統編碼,可能是各種形式編碼。關鍵一句:
request.setCharacterEncoding("UTF-8")。
網上有此filter的源碼,Jdon框架源碼中com.jdon.util.SetCharacterEncodingFilter
需要配置web.xml 激活該Filter。
3. 在JSP頭部聲明:<%@ page contentType="text/html;charset= UTF-8" %>。
4. 在Jsp的html代碼中,聲明UTF-8:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
5. 設定數據庫連接方式是UTF-8。例如連接MYSQL時配置URL如下:
jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8
一般數據庫都可以通過管理設置設定UTF-8
6. 其他和外界交互時能夠設定編碼時就設定UTF-8,例如讀取文件,操作XML等。

       

  以上討論了Java/J2EE的中文問題。如果整個應用系統是從開始進行開發,那麼統一指定編碼爲UTF-8就非常容易做到。如果是在英文源代碼基礎上二次開發,那麼首先要將原來的源代碼轉換爲統一編碼UTF-8,那麼這種轉換工作會帶來一定的麻煩。
  

  有了這個解決方案,無論使用什麼框架Struts 或JSF或未來出現的Java技術,統一成UTF-8的方案都不會出現亂碼,筆者以前在Jsp/Servlet時就基於這個原則,後來使用Struts等框架,從未被亂碼困擾過,希望本方案公佈出來供更多初學者分享,減少Java/J2EE的第一個攔路虎,也避免採取一些臨時解決方案。


***********************************************************************************************************************
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>
org.springframework.web.filter.CharacterEncodingFilter
</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>

<filter-mapping>
<filter-name>encodingFilter</filter-name>
<servlet-name>action</servlet-name>
</filter-mapping>


***********************************************************************************************

hibernate+mysql寫入數據庫的中文是亂碼,怎麼解決??


hibernate.hbm.xml加上屬性.
<property name=\"connection.useUnicode\">true</property>
<property name=\"connection.characterEncoding\">UTF-8</property>

mysql 的驅動用3.0.15以上版本的,

加個Filter, 使用UTF-8字符集就可以了,





1.使ApplicationResources.properties支持中文
建立一個ApplicationResources_ISO.properties文件,把應用程序用的message都寫進去,然後在dos下執行這個命令,
native2ascii -encoding gb2312 ApplicationResources_ISO.properties ApplicationResources.properties
這樣就會將ISO編碼的ApplicationResources轉換成GB2312編碼的格式了,同時保存到ApplicationResources.properties.
native2ascii這個工具是jdk自帶的一個東東,所以如果path都設定正確就可以直接運行了,你可以在$java_home$/bin下找到他。
轉換後的中文類似於這個樣子
iso 格式下 :tj.type=商品車類型
gb2312格式下 :tj.type=\u5546\u54c1\u8f66\u7c7b\u578b
然後在struts-config.xml中設置應用這個資源文件
<message-resources parameter=\"com.huahang.tj.ApplicationResources\" key=\"org.apache.struts.action.MESSAGE\" />
開發jsp時在jsp的開頭寫上<%@ page contentType=\"text/html; charset=gb2312\" %>,將字符集設置成gb2312就可以了。

2.使數據庫操作支持中文。
數據庫操作支持中文一直讓我比較頭痛,但是感謝善解人衣向我推薦了www.chinaxp.org,這個網站是用struts框架開發的,而且
開放源碼,下載了源碼後發現它的中文處理得很好,閱讀部分源碼,沒有發現什麼特殊的字符集轉換,很納悶,偶然看到樓上網友
留言知道原來servlet可以統一設置字符轉換。chinaxp.org就是這麼做的。
在web.xml中加上
<filter>
<filter-name>Set Character Encoding</filter-name>
<filter-class>com.huahang.tj.struts.filters.SetCharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>GB2312</param-value>
</init-param>
<init-param>
<param-name>ignore</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>Set Character Encoding</filter-name>
<servlet-name>action</servlet-name>
</filter-mapping>
這裏會涉及一個bean,源碼如下:
/*
* XP Forum
*
* Copyright (c) 2002-2003 RedSoft Group. All rights reserved.
*
*/
package com.huahang.tj.struts.filters;

import javax.servlet.*;
import java.io.IOException;

/**
* <p>Filter that sets the character encoding to be used in parsing the
* incoming request, either unconditionally or only if the client did not
* specify a character encoding. Configuration of this filter is based on
* the following initialization parameters:</p>
* <ul>
* <li><strong>encoding</strong> - The character encoding to be configured
* for this request, either conditionally or unconditionally based on
* the <code>ignore</code> initialization parameter. This parameter
* is required, so there is no default.</li>
* <li><strong>ignore</strong> - If set to \"true\", any character encoding
* specified by the client is ignored, and the value returned by the
* <code>selectEncoding()</code> method is set. If set to \"false,
* <code>selectEncoding()</code> is called <strong>only</strong> if the
* client has not already specified an encoding. By default, this
* parameter is set to \"true\".</li>
* </ul>
*
* <p>Although this filter can be used unchanged, it is also easy to
* subclass it and make the <code>selectEncoding()</code> method more
* intelligent about what encoding to choose, based on characteristics of
* the incoming request (such as the values of the <code>Accept-Language</code>
* and <code>User-Agent</code> headers, or a value stashed in the current
* user\'s session.</p>
*
* @author <a href=\"mailto:[email protected]\">John Wong</a>
*
* @version $Id: SetCharacterEncodingFilter.java,v 1.1 2002/04/10 13:59:27 johnwong Exp $
*/
public class SetCharacterEncodingFilter implements Filter {

// ----------------------------------------------------- Instance Variables


/**
* The default character encoding to set for requests that pass through
* this filter.
*/
protected String encoding = null;


/**
* The filter configuration object we are associated with. If this value
* is null, this filter instance is not currently configured.
*/
protected FilterConfig filterConfig = null;


/**
* Should a character encoding specified by the client be ignored?
*/
protected boolean ignore = true;


// --------------------------------------------------------- Public Methods


/**
* Take this filter out of service.
*/
public void destroy() {

this.encoding = null;
this.filterConfig = null;

}


/**
* Select and set (if specified) the character encoding to be used to
* interpret request parameters for this request.
*
* @param request The servlet request we are processing
* @param result The servlet response we are creating
* @param chain The filter chain we are processing
*
* @exception IOException if an input/output error occurs
* @exception ServletException if a servlet error occurs
*/
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain)
throws IOException, ServletException {

// Conditionally select and set the character encoding to be used
if (ignore || (request.getCharacterEncoding() == null)) {
String encoding = selectEncoding(request);
if (encoding != null)
request.setCharacterEncoding(encoding);
}

// Pass control on to the next filter
chain.doFilter(request, response);

}


/**
* Place this filter into service.
*
* @param filterConfig The filter configuration object
*/
public void init(FilterConfig filterConfig) throws ServletException {

this.filterConfig = filterConfig;
this.encoding = filterConfig.getInitParameter(\"encoding\");
String value = filterConfig.getInitParameter(\"ignore\");
if (value == null)
this.ignore = true;
else if (value.equalsIgnoreCase(\"true\"))
this.ignore = true;
else if (value.equalsIgnoreCase(\"yes\"))
this.ignore = true;
else
this.ignore = false;

}


// ------------------------------------------------------ Protected Methods


/**
* Select an appropriate character encoding to be used, based on the
* characteristics of the current request and/or filter initialization
* parameters. If no character encoding should be set, return
* <code>null</code>.
* <p>
* The default implementation unconditionally returns the value configured
* by the <strong>encoding</strong> initialization parameter for this
* filter.
*
* @param request The servlet request we are processing
*/
protected String selectEncoding(ServletRequest request) {

return (this.encoding);

}

}//EOC
加上這個後,在action中就可以直接從form中接收gb2312編碼的數據了,返回時自然也是gb2312了。
但是這個好像需要servlet 2.2以上的容器

綜合上面的方法,我解決了struts中的中文問題,現在還沒發現新的問題。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章