Grails does not properly encode unicode characters

In my grails application, Unicode characters are incorrectly encoded.

I am using grails 1.3.7 and tomcat 7.0.22. Below are the settings that I configured in my application 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.

But still, when I send a Unicode character, grails does not support the character, and the distorted value is preserved. I googled around and read ppl asking for help on the same issue, but unfortunately the solutions do not work for my benefit. Although, I found a workaround for this problem. Next expression

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

will correctly encode Unicode character.

However, using this approach is tedious since I will have to do this with all the text inputs in my application. Why is the Unicode character incorrectly encoded with graals and / or tomcat? I think I have the correct settings.

+4
source share
2 answers

If you do not use Mysql, but HSqlDB, which is sent by default, you will not see your encoding problems. Problems arise due to Mysql, InnoDB and UTF-8.

Since you are using a Mysql connection and have already configured useUnicode=true&characterEncoding=UTF-8to MySql connection url

you still have to add special hibernation dialogs for InnoDB and UTF-8:

Datasource.groovy must contain:

environments {
    development {
        dataSource {         
            ......
            driverClassName = "com.mysql.jdbc.Driver"
            dialect = "com.domain.mysql.dialect.MySQLUTF8InnoDBDialect"
            .....

Create a new file in 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";
    }
}

Make sure yours Config.groovyhas:

grails.views.default.codec = "html"
grails.views.gsp.encoding = "UTF-8"
grails.converters.encoding = "UTF-8"

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

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

hello

Jan

+3

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;
+1

All Articles