tomcat+SSH中遇到中文乱码的解决方法

时间:2023-01-07 21:24:05

最近开始又转向J2EE的开发。很久没用,重新熟悉。


本地环境ubuntu 12.13,mysql5.x,tomcat7.x,struts2.3.15.x,spring3.1.0,hibernate4.1.x


之前一直连线上服务器上测试数据库,没有发现有中文乱码问题。

现在在本地搭建此环境,使用本地数据库,便于调试,但是发现每次对数据库操作,有中文就会出现????乱码。


那现在一步步排除什么原因引起:

  1. 前端jsp或者html界面:
对于在页面中显示出现乱码,这个问题比较简单,便是检查你的JSP文件里是不是出现了中文要处理,因为JSP默认的编码格式为“ISO-8859-1”,当JSP中出现要处理的中文时,其显示就出现乱码了,这种情况一般出现在手写JSP,或修改时。
      
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="UTF-8"%>
     2、传参数过程中乱码
        传递参数出现的乱码,参数的内容为中文。无论是post或者get。

        需要关注的位置:
  •  tomcat 配置,位置 tomcat->conf->server.xml,加入URIEncoding="utf-8"
 <Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443"
URIEncoding="utf-8"/>

  • web.xml中加入过滤器filter
<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>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

 上面项目中用的spring带的web字符过滤器
         
  • tomcat 配置,位置 tomcat->conf->web.xml     放开注释的过滤器
           
<!-- A filter that sets character encoding that is used to decode -->
<!-- parameters in a POST request -->
<filter>
<filter-name>setCharacterEncodingFilter</filter-name>
<filter-class>org.apache.catalina.filters.SetCharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<async-supported>true</async-supported>
</filter>

  • 编写过滤器Filter(这个没用到)
       3、后台部分
       主要是关注:hibernate和mysql(我项目用的mysql数据库)
  •  hibernate注意:
         连接mysql,如果用hibernate.cfg.xml加上属性. 
<property name="connection.useUnicode">true</property> 
<property name="connection.characterEncoding">UTF-8</property> 
        如果用spring配置文件:
        有两种方法,1、直接写到applicationContext.xml中;2、写到database-config.properties中,由applicationContext.xml调用
        database-config.properties:
      
mydata.driverClass=com.mysql.jdbc.Driver
#mydata.url=jdbc:mysql://localhost:3306/sdk?useUnicode=true&amp;characterEncoding=UTF-8
mydata.url=jdbc:mysql://localhost:3306/sdk?useUnicode=true&characterEncoding=UTF-8
mydata.username=root
wshouyouDB.password=xxxx
      (我遇到的问题是出现上段代码中)
       applicationContext.xml:
<!-- 读取jdbc.properties配置文件 -->
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<value>classpath:database-config.properties</value>
</property>
</bean>
<bean id="dataSource" destroy-method="close"
class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${mydata.driverClass}" />
<property name="jdbcUrl" value="${mydata.url}" />
<property name="user" value="${mydata.username}" />
<property name="password" value="${mydata.password}" />

  • mysql部分
          创建数据库表中,注意:
         show create table gamepad_model;看一下创建表的语句
         
CREATE TABLE `gamepad_model` (
`edit_id` varchar(50) NOT NULL,
`filedata` text,
`gamepad_type` varchar(100) NOT NULL,
`pic_url` varchar(255) DEFAULT NULL,
`sdk_version` varchar(50) NOT NULL,
`update_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`edit_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
      需要加上: CHARSET=utf8

     排除是不是mysql的问题,可以手动插入一条含有中文的数据,看看表中是否有乱码;
    
     
insert into gamepad_model(edit_id,filedata,gamepad_type,sdk_version) values('test3_测试','ttt','testtt_你好','1');
     如果没有问题,则排除mysql问题
     

     一步一步排除

    1、前台,是否有乱码,不是,传递参数层
    2、传递参数,到java中,看到传递过来的中文,debug下是否有中文乱码,如果没有,排除之
    3、前两步排除后,就应该是后台问题,hibernate或者mysql问题,
    4、可以先看上边提到的,手动插入一句有中文内容的语句,看数据库中是否有乱码,如果没有,排除之
    5、到这一步,就只剩下hibernate问题了。在上文中,提到注意的地方,检查。
    我的问题就在这一步, 
mydata.url=jdbc:mysql://localhost:3306/sdk?useUnicode=true&characterEncoding=UTF-8
   一般不在xml中,连接数据库需要加上
useUnicode=true&characterEncoding=UTF-8
   但是如果在xml中,需要转意一下
 mydata.url=jdbc:mysql://localhost:3306/sdk?useUnicode=true &amp;characterEncoding=UTF-8
   
    
   其他方面没有研究,有的话,请多指教!