Struts+Hibernate+MySql 中文亂碼問題解決方案
項目:china
數據庫版本:4.0.23
幾個關鍵地點:
一、MySql數據庫:配置my.ini文件,內容如下:
#This File was made using the WinMySQLAdmin 1.4 Tool
#2005-10-25 17:36:48
#Uncomment or Add only the keys that you know how works.
#Read the MySQL Manual for instructions
[mysqld]
basedir=D:/mysql
#bind-address=192.168.0.23
datadir=D:/mysql/data
default-character-set=gb2312
#language=D:/mysql/share/your language directory
#slow query log#=
#tmpdir#=
#port=3306
#set-variable=key_buffer=16M
[WinMySQLadmin]
Server=D:/mysql/bin/mysqld-nt.exe
user=root
password=root
紅色標註處爲MySql缺省字符集配置
二、Hibernate.cfg.xml配置文件
在JDBC的URL處寫:
jdbc:mysql://localhost:3306/china?useUnicode=true&characterEncoding=gb2312
此處由於環境問題在我的機器上不寫“?useUnicode=true&characterEncoding=gb2312”也可以通過,但是在其他同學機器上就會出現亂碼,保守一點寫上比較好。
三、struts.action.NameAction.java
package struts.action;
import java.io.UnsupportedEncodingException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import struts.form.NameForm;
import bo.NameFacade;
import po.Name;
/**
* MyEclipse Struts
* Creation date: 12-26-2005
*
* XDoclet definition:
* @struts:action path="/name" name="nameForm" input="/form/name.jsp" scope="request" validate="true"
*/
public class NameAction extends Action {
// --------------------------------------------------------- Instance Variables
// --------------------------------------------------------- Methods
/**
* Method execute
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
*/
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) {
//response.setContentType("text/html;charset=UTF-8");
/*try {
request.setCharacterEncoding("gb2312");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}*/
NameForm nameForm = (NameForm) form;
String name = nameForm.getName();
if(name!=null)
try {
name = new String(name.getBytes("ISO-8859-1"),"gb2312");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("+++action+++"+name);
NameFacade nameFacade = new NameFacade();
Name namedb = new Name();
namedb.setName(name);
nameFacade.addUser(namedb);
// TODO Auto-generated method stub
return mapping.findForward("suc");
}
}
紅色代碼段爲從用戶表單所得到的數據並經過Form,其爲“ISO-8859-1”編碼,將其用getBytes方法轉換成“gb2312”編碼。
四、china.jsp
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>My JSP 'china.jsp' starting page</title>
</head>
<body>
<form name="form1" method="post" action="/china/name.do">
<input type="text" name="name">
<input type="submit" name="Submit" value="提交">
</form>
<a href="/china/view.do">查看</a>
</body>
</html>
紅色標註處統一爲“gb2312”編碼。
五、view.jsp 如上修改,另外struts.form.NameForm.java不需要任何修改。
備註:經測試,增加、查看兩相操作沒有發現中文亂碼問題
數據庫中的數據應爲轉碼後的數據,轉碼前的數據有待進一步測試
最後說一下我理解的數據流向:
瀏覽器IE,JSP&HTML ——> Form ——> Action ——> BO ——> PO ——> DB
(ISO-8859-1) (ISO-8859-1) (ISO-8859-1——>gb2312) (gb2312) (gb2312) (gb2312)
如有問題發生,歡迎批評指教!