日期:2014-05-16 浏览次数:20922 次
标签:
杂谈 |
分类: struts2 |
目前对遇到jsp中文及其乱码问题做个总结,方便自己也方便他人. 主要是5大配置点:
1 struts2配置 2 数据库 3 页面 4 jdbc连接 5 tomcat
以下解决方法不分顺序,大家按照自己的配置对比.
1 struts2配置
?struts2在配置struts.properties文件中加上struts.i18n.encoding=UTF-8?或者在 struts.xml里加上<constant name="struts.i18n.encoding" value="UTF-8" /> 有的朋友说加上struts.locale=zh_CN,但是我的struts.locale为en_GB没有问题.
2 数据库
2.1 mysql的表的编码方式,其DDL应该为: ENGINE=InnoDB DEFAULT CHARSET=utf8; 可以使用show create table tbname查看.
2.2 安装mysql数据库时就应该以utf8编码默认安装查看mysql数据库默认编码方式可以在其目录下my.ini文件中找到mysql和mysqld下default-character-set值为utf8或者在mysql数据库命令行下以"\s;"查看.
3 页面
每个页面上加上<%@page pageEncoding="UTF-8" contentType="text/html; charset=UTF-8" %> 这样在jsp页面里,点右键,查看编码方式则为UTF-8.
4 jdbc连接
?设置characterencoding为UTF-8 如jdbc.mysql.url=jdbc:mysql://localhost:3306/db?useUnicode=true&characterEncoding=UTF8
5 tomcat
为了保证get/post数据都采用相同的UTF8编码,我们在server.xml中进行了如下设置: <Connector port="8080" protocol="HTTP/1.1" ?????????????? connectionTimeout="20000" ?????????????? redirectPort="8443"? URIEncoding="UTF-8" />
其中,当当使用IIS作为webserver转发servlet/jsp请求时IIS是通过AJP协议,把请求转发到Tomcat监听的8009端口,所以需要配置端口8009的URIEncoding为UTF-8.
???? msyql数据库安装时候需要默认编码方式或者使用MySQL Server Instance Config Wizard 配置,
在设置Configure the MySQL Server 5.0 server instance那里的时候选择Manual Selected Default Character Set / Collation 为 UTF8 而不是默认的 LATIN1 在MYSQL里查看得 mysql> \s;
-------------- E:\MySQL\MySQL Server 5.0\bin\mysql.exe? Ver 14.12 Distrib 5.0.51a, for Win32 (ia32) Connection id:? ? ? ? ? 2 Current database: Current user:? ? ? ? ? root@localhost SSL:? ? ? ? ? ? ? ? ? ? Not in use Using delimiter:? ? ? ? ; Server version:? ? ? ? 5.0.51a-community-nt MySQL Community Edition (GPL) Protocol version:? ? ? 10 Connection:? ? ? ? ? ? localhost via TCP/IP Server characterset:? ? utf8 Db? ? characterset:? ? utf8 Client characterset:? ? utf8 Conn.? characterset:? ? utf8 TCP port:? ? ? ? ? ? ? 3306 Uptime:? ? ? ? ? ? ? ? 25 sec Threads: 1? Questions: 4? Slow queries: 0? Opens: 12? Flush tables: 1? Open tables: 6? Queries per second avg: 0.160
??? my.ini文件内容为
[mysql] default-character-set=utf8 # SERVER SECTION # ---------------------------------- # # The following options will be read by the MySQL Server. Make sure that # you have installed the server correctly (see above) so it reads this # file. # [mysqld] # The TCP/IP Port the MySQL Server will listen on port=3306 #Path to installation directory. All paths are usually resolved relative to this. basedir="E:/MySQL/MySQL Server 5.0/" #Path to the database root datadir="E:/MySQL/MySQL Server 5.0/Data/" # The default character set that will be used when a new schema or table is # created and no character set is defined default-character-set=utf8 ...........