我有UTF-8的问题.我使用
Spring MVC和
MySQL.
在Spring中,我已经配置了web.xml:
encodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
UTF-8
forceEncoding
true
encodingFilter
/*
在我的bean数据源中,我有:
class="it.roundtable.db.manager.CustomDataSource" init-method="init">
CustomDataSource的位置是:
import org.apache.tomcat.dbcp.dbcp.BasicDataSource;
public class CustomDataSource extends BasicDataSource {
private void init() {
addConnectionProperty("useUnicode", "true");
addConnectionProperty("characterEncoding", "UTF-8");
}
}
在我的布局页面中,我有:
在MySQL我有:
mysql> SHOW VARIABLES LIKE 'char%';
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.08 sec)
和
mysql> SHOW VARIABLES LIKE 'colla%';
+----------------------+-----------------+
| Variable_name | Value |
+----------------------+-----------------+
| collation_connection | utf8_general_ci |
| collation_database | utf8_general_ci |
| collation_server | utf8_general_ci |
+----------------------+-----------------+
3 rows in set (0.00 sec)
在my.cnf:
[client]
port = 3306
socket = /var/run/mysqld/mysqld.sock
default-character-set=utf8
....
[mysqld]
character-set-server=utf8
default-character-set=utf8
default-collation=utf8_general_ci
init-connect='SET NAMES utf8'
character-set-client=utf8
skip-external-locking
....
现在,当我在表格中写下这样一句话时:
This is a prove: àèìòù
在Spring控制器中我打印在日志中:
...
logger.info("text --> " + text);
...
并在eclipse控制台中打印的结果是正确的.
然后我将我的文本保存在带有数据源的mysql表中,如果我用bash连接到mysql,我可以读取保存在表中的正确文本.
在下一步中,spring控制器通过查询从mysql中检索数据,但我可以从logger(eclipse控制台)看到字母“àèìòù”编码错误:
This is a prove: �����
所以我的网页结果是一样的.
你能帮我吗?
我发现了错误:我用来保存文本的类型是BLOB,现在我使用TEXT类型,一切都很完美.
抱歉!