struts+hibernate+MySql中文亂碼解決方案

來源:互聯網
上載者:User

出自: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=3306default-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&amp;characterEncoding=UTF-8
 </property>  本操作步驟在struts1.2 + hibernate 3.1 +mysql 5.0 Windows2000平台上通過,可解決亂碼問題,其他的資料庫請讀者自行測試

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.