tomcat+SSH中遇到中文乱码的解决方法
来源:互联网 发布:gpt efi ubuntu 编辑:程序博客网 时间:2024/05/02 22:22
SSH框架的搭建详细图文教程 请前往下载
之前一直连线上服务器上测试数据库,没有发现有中文乱码问题。
现在在本地搭建此环境,使用本地数据库,便于调试,但是发现每次对数据库操作,有中文就会出现????乱码。
那现在一步步排除什么原因引起:
对于在页面中显示出现乱码,这个问题比较简单,便是检查你的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(这个没用到)
- hibernate注意:
<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&characterEncoding=UTF-8mydata.url=jdbc:mysql://localhost:3306/sdk?useUnicode=true&characterEncoding=UTF-8mydata.username=rootwshouyouDB.password=xxxx(我遇到的问题是出现上段代码中) applicationContext.xml:
<!-- 读取jdbc.properties配置文件 --><beanclass="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部分
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&characterEncoding=UTF-8 其他方面没有研究,有的话,请多指教!
0 0
- tomcat+SSH中遇到中文乱码的解决方法
- tomcat+SSH中遇到中文乱码的解决方法
- tomcat+SSH中遇到中文乱码的解决方法
- tomcat+SSH中遇到中文乱码的解决方法
- Tomcat'sURL中传递中文参数乱码的解决方法!
- Tomcat中应用日志中文乱码解决方法
- JSON遇到中文乱码问题的解决方法
- JSON遇到中文乱码问题的解决方法
- tomcat中文乱码的另类解决方法
- tomcat中文乱码的另类解决方法
- Tomcat中文乱码问题的解决方法
- ssh框架整合中jsp向后台传参数是中文遇到乱码的简便解决方案
- JSP中获取HTML中的中文内容是乱码的解决方法---开发中遇到的问题
- Struts2文件下载遇到中文乱码、中文丢失的解决方法
- tomcat 中文乱码filter解决方法
- 项目中遇到的中文乱码问题
- 学习中遇到的中文乱码问题。
- ssh客户端登陆linux中文乱码的解决方法
- oracle 12C 软件克隆安装
- pch错误
- jeesz javaWeb分布式架构使用技术分享
- eclipse 使用maven 搭建 springboot+mybatis + 多数据源
- Nifi源码解读
- tomcat+SSH中遇到中文乱码的解决方法
- ubuntu上disable intel_pstate,使用固定频率
- jQuery代码优化 事件委托篇
- CentOS 6.6 安装Python 2.7.8
- LeetCode : Ugly Number
- Java序列化-实例
- 前面三周回顾
- logd 删除log
- oracle 10g 各版本及补丁下载地址