在我的grails应用程序中,unicode字符没有正确编码。
我使用的是grails 1.3.7和tomcat 7.0.22。以下是我在应用程序中为unicode支持配置的设置:
- Set grails.views.gsp.encoding and grails.converters.encoding="UTF-8" in Config.groovy
- Set encoding to UTF-8 in meta tag in the .gsp pages
- Specified 'useUnicode=true&characterEncoding=UTF-8' to the MySql connection URL (DB has characterset set to UTF-8)
- Set URIEncoding="UTF-8" useBodyEncodingForURI="true" to the server.xml file in tomcat
- Specified the attribute accept-charset="UTF-8" of the form tag.但是,当我提交一个unicode字符时,grails不支持该字符,并且乱码将被保存。我在谷歌上到处搜索,并阅读了人们在这个问题上寻求帮助的文章,但不幸的是,解决方案对我并不起作用。尽管如此,我还是找到了解决这个问题的办法。下面的表达式
params.detail = params.detail ? new String(params.detail.getBytes("8859_1"), "UTF8") : null将对unicode字符进行正确编码。
然而,使用这种方法很乏味,因为我将不得不对我的应用程序中的所有文本输入执行此操作。为什么grails和/或tomcat不能正确编码unicode字符?我想我的设置是正确的。
发布于 2012-05-09 20:15:05
如果您使用的不是Mysql,而是默认附带的HSqlDB,则不会出现编码问题。出现问题的原因是Mysql、InnoDB和UTF-8。
由于您正在使用Mysql连接,并且已经将useUnicode=true&characterEncoding=UTF-8设置为MySql连接URL
你仍然需要为InnoDB和UTF-8添加一个特殊的hibernate方言:
Datasource.groovy应包含:
environments {
development {
dataSource {
......
driverClassName = "com.mysql.jdbc.Driver"
dialect = "com.domain.mysql.dialect.MySQLUTF8InnoDBDialect"
.....在src/java/com/domain/mysql/dialect/MySQLUTF8InnoDBDialect.java中创建新文件
package com.domain.mysql.dialect;
import org.hibernate.dialect.MySQLInnoDBDialect;
/**
* Sets the default charset to UTF-8.
*/
public class MySQLUTF8InnoDBDialect extends MySQLInnoDBDialect {
@Override
public String getTableTypeString() {
return " ENGINE=InnoDB DEFAULT CHARSET=utf8";
}
}确保您的Config.groovy具有:
grails.views.default.codec = "html"
grails.views.gsp.encoding = "UTF-8"
grails.converters.encoding = "UTF-8"而您的视图/布局/main.gsp以:
<%@ page contentType="text/html;charset=UTF-8" %>大家好,
1月
发布于 2012-05-11 09:06:02
将方言设置为UTF-8是行不通的。您还必须修改表或表中的列以支持UTF-8。
下面是更改该表的命令
ALTER TABLE tbl_name CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;或者,您可以尝试只修改要存储utf-8的列
使用命令
ALTER TABLE t MODIFY col1 CHAR(50) CHARACTER SET utf8;https://stackoverflow.com/questions/10511497
复制相似问题