Grails 没有正确编码 unicode 字符 [英] Grails not encoding the unicode characters properly

查看:20
本文介绍了Grails 没有正确编码 unicode 字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 grails 应用程序中,unicode 字符没有正确编码.

In my grails app, the unicode characters are not being encoded properly.

我使用的是 grails 1.3.7 和 tomcat 7.0.22.以下是我在我的应用中为 unicode 支持配置的设置:

I'm using grails 1.3.7 and tomcat 7.0.22. Following are the settings that I've configured in my app for unicode support:

- 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 不支持该字符并且正在保存乱码值.我已经用谷歌搜索并阅读了 ppl 在同一问题上寻求帮助,但不幸的是,这些解决方案对我不利.尽管如此,我已经找到了解决此问题的方法.下面的表达式

But still, when I submit a unicode character, grails doesn't support the character and the garbled value is being saved. I've googled around and have read ppl asking for help on this same issue but unfortunately the solutions don't work for my favor. Although, I've found a workaround to this problem. The following expression

params.detail = params.detail ? new String(params.detail.getBytes("8859_1"), "UTF8") : null

将正确编码 unicode 字符.

will correctly encode the unicode character.

然而,使用这种方法很乏味,因为我必须对我的应用程序中的所有文本输入都这样做.为什么 unicode 字符没有被 grails 和/或 tomcat 正确编码?我想我的设置正确.

However, using this approach is tedious since I will have to do this to all the text inputs in my app. Why is the unicode character not being properly encoded by grails and/or tomcat? I think I have the correct settings.

推荐答案

如果你使用的不是 Mysql 而是默认的 HSqlDB,你将看不到你的编码问题.出现问题的原因是Mysql、InnoDB和UTF-8.

If you are not using Mysql but the HSqlDB which is shipped as default, you will not see your encoding problems. The problems occurs due to Mysql, InnoDB and UTF-8.

由于您使用的是 Mysql 连接并且已经设置useUnicode=true&characterEncoding=UTF-8 到 MySql 连接 URL

Since you are using a Mysql Connection and already setting useUnicode=true&characterEncoding=UTF-8 to the MySql connection URL

您仍然需要为 InnoDB 和 UTF-8 添加特殊的休眠方言:

you still have to add a special hibernate dialect for InnoDB and UTF-8:

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"

你的 views/layouts/main.gsp 以:

And your views/layouts/main.gsp starts with:

<%@ page contentType="text/html;charset=UTF-8" %>

您好,

一月

这篇关于Grails 没有正确编码 unicode 字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆