0
votes

I've got a Grails (2.0.4) application, all setted up to manage UTF-8 encoding (meta tag in the layout, mysql database tables). Unfortunately, something strange happens.

For example, if in a form (to create a domain instance) I type any text containing non-UK characters, like this:

más que nada

the POST contains the exact text (with the "á" character as is) but the params variable in the controller contains the wrong text:

más que nada

There's nothing between the view and the controller, how can this happen?

I also tried, without good results, to set in Config.groovy:

grails.views.default.codec = "html"

Is there something else I'm missing to set up?

Thanks in advance to everyone who will take the time to have a look at this issue.

6

6 Answers

1
votes

How about these values in your Config.groovy:

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

Are those properly configured?

0
votes

On prod I have configured my tomcat 6 in server.xml as
<Connector port="14080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="14443" URIEncoding="UTF-8"/>
The most important line is URIEncoding="UTF-8"

0
votes

What's the default charset of your MySQL database? Is it ok?

This is how I create my MySQL databases:

create database [dbname] DEFAULT CHARACTER SET = utf8 DEFAULT COLLATE utf8_swedish_ci;

see http://dev.mysql.com/doc/refman/5.5/en/create-database.html for full syntax of CREATE DATABASE

Collation affects sorting. You can get a list with "show collation" sql statement in mysql. http://dev.mysql.com/doc/refman/5.1/en/show-collation.html

Changing an existing table's encoding is done with this command:

ALTER TABLE tbl_name CONVERT TO CHARACTER SET charset_name [COLLATE collation_name];

You can check the encoding of an existing table with the "show create table tbl_name" command. Changing the default encoding of the database doesn't change the encoding of existing tables (or tables imported from a mysql dump).

0
votes

Did you already try with

${myHtmlContent.encodeAsHtml()}

in your view?

0
votes

Well this post is few months old and the OP might possibly have found out a better solution. But an alternative solution to this problem that I have managed is to explicitly change the character encoding of the parameter in concern.

For instance, params.paramsname = new String(params.unicodeInput.getBytes("8859_1"), "UTF8");

This will force the paramsname to be correctly decoded to the Unicode character.

I just ran into this problem and just to remind you that it's just a workaround. I'm still looking for a better solution too. cheerzzz!

0
votes

I'm sorry, I figured out what the problem was days ago but I hadn't time to answer my own question till now.

Unfortunately I forgot to mention a key part of the problem, because I didn't thought it was related. I got the encoding problem only on AJAX call, and I didn't mention it because all savings in my application are done through AJAX.

So, the encoding problem was related to the configuration of the content type of the jQuery post, which (to work properly with UTF-8) has to be like this:

contentType: "application/x-www-form-urlencoded;charset=UTF-8"