出自:http://citszhanghj.spaces.live.com/blog/cns!d7f04e581faf210!390.entry
struts+hibernate+MySql中文乱码解决方案
1、修改MySql数据库的my.ini配置文件、
# CLIENT SECTION
# ----------------------------------------------------------------------
#
# The following options will be read by MySQL client applications.
# Note that only client applications shipped by MySQL are guaranteed
# to read this section. If you want your own MySQL client program to
# honor these values, you need to specify it as an option during the
# MySQL client library initialization.
#
[client]
port=3306
default-character-set=GBK
#此处默认编码修改为GBK或utf-8
# SERVER SECTION
# ----------------------------------------------------------------------
#
2、创建数据表的时候
# 主机: localhost
# 数据库: cits
# 表: 'article'
#
CREATE TABLE `article` (
`article_id` varchar(100) NOT NULL default '0',
`article_title` varchar(100) default NULL,
`domain_id` varchar(100) default NULL,
`article_text` varchar(100) default '',
`good_flg` tinyint(1) default NULL,
`lock_flg` tinyint(1) default NULL,
`top_flg` tinyint(1) default NULL,
`read_times` int(11) default NULL,
`reply_times` int(11) default NULL,
`last_reply` char(6) default NULL,
`last_reply_time` datetime default NULL,
`add_user` varchar(30) default NULL,
`add_date` datetime default NULL,
`upd_user` varchar(30) default NULL,
`upd_date` datetime default NULL,
PRIMARY KEY (`article_id`)
) ENGINE=MyISAM DEFAULT CHARSET=gbk;
此处要用GBK编码
3、在jsp页面中使用UTF-8编码
<%
Object init = request.getAttribute("init");
String domainId = (String)request.getAttribute("domainId");
if ((init == null) || (init.equals("")) || (init.equals("null"))) {
response.sendRedirect("./listArticle.do?domainId="+domainId);
} else {
%>
<%@ page language="java" import="java.util.*"
pageEncoding="UTF-8"%>
<%@ page import ="java.util.HashMap"%>
<%@ page import ="java.util.List"%>
<%@ page import ="bo.*" %>
<%
List articleList = (List)request.getAttribute("articleList");
//String domainId = (String)request.getAttribute("domainId");
int size = 0;
if(articleList !=null){
size = articleList.size();
}
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>帖子列表</title>
</head>
<body>
<p align="center"><a href="articleSave.jsp?domainId=<%=domainId%>">发表新主题</a></p>
<table width="800" border="1" align="center" cellpadding="0" cellspacing="0" bordercolor="#6666FF">
<tr>
<td bgcolor="#6666FF"><div align="center">回/点击</div></td>
<td bgcolor="#6666FF"><div align="center">标题</div></td>
<td bgcolor="#6666FF"><div align="center">发表时间</div></td>
<td bgcolor="#6666FF"><div align="center">发帖人</div></td>
<td bgcolor="#6666FF"><div align="center">最后回复</div></td>
</tr>
<%for(int i = 0;i<size;i++){%>
<tr>
<td><div align="center"><%=((Article)articleList.get(i)).getReplyTimes()%><br>
<%=((Article)articleList.get(i)).getReadTimes()%></div></td>
<td><div align="center"><%=((Article)articleList.get(i)).getArticleTitle()%></div></td>
<td><div align="center"><%=((Article)articleList.get(i)).getAddDate()%></div></td>
<td><div align="center"><%=((Article)articleList.get(i)).getAddUser()%></div></td>
<td><div align="center"><%=((Article)articleList.get(i)).getLastReply()%></div></td>
</tr>
<%}%>
</table>
</body>
</html>
<%}%>
4、给Web应用加上过滤器,做一个Fiter,对所有请求统一使用UTF-8编码
下面是Fiter的源代码
package com.util.common;
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 EncodingFilter implements Filter {
protected String encoding = null;
protected FilterConfig filterConfig = null;
protected boolean ignore = true;
public void init(FilterConfig filterConfig) throws ServletException {
// TODO Auto-generated method stub
this.filterConfig = filterConfig;
this.encoding = filterConfig.getInitParameter("encoding");
System.out.println("====initEncodingFilter");
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;
}
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
// TODO Auto-generated method stub
if(ignore||(request.getCharacterEncoding() == null)){
String encoding = selectEncoding(request);
if(encoding!=null){
request.setCharacterEncoding(encoding);
}
}
chain.doFilter(request,response);
}
public void destroy() {
// TODO Auto-generated method stub
this.encoding = null;
this.filterConfig = null;
}
protected String selectEncoding(ServletRequest request){
return(this.encoding);
}
}
5、在Web.xml中配置过滤器,使之能起过滤作用
在web.xml中加入下面代码
<filter>
<filter-name>Encoding</filter-name>
<filter-class>com.util.common.EncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>Encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
6、在hibernate.cfg.xml里连接数据的属性项修改成如下代码
<property name="connection.url">
jdbc:mysql://localhost:3306/cits?useUnicode=true&characterEncoding=UTF-8
</property>
本操作步骤在struts1.2 + hibernate 3.1 +mysql 5.0
Windows2000平台上通过,可解决乱码问题,其他的数据库请读者自行测试