Grails不能正确编码unicode字符 [英] Grails not encoding the unicode characters properly
问题描述
在我的grails应用程序中,unicode字符未被正确编码。
我使用的是grails 1.3.7和tomcat 7.0.22。以下是我在应用程序中为unicode支持配置的设置:
- 设置grails.views.gsp.encoding和Config.groovy中的grails.converters.encoding =UTF-8
- 在.gsp页面的元标记中将编码设置为UTF-8
- 指定'useUnicode = true& characterEncoding = UTF-8 '到MySql连接URL(DB具有设置为UTF-8的字符集)
- 在tomcat中为server.xml文件设置URIEncoding =UTF-8useBodyEncodingForURI =true
- 指定属性表单标签的accept-charset =UTF-8。
但是,当我提交unicode字符时,grails不支持字符和乱码值正在被保存。我搜索了一下,并且已经阅读了关于这个问题的请求帮助,但不幸的是,这些解决方案对我不利。虽然,我已经找到了解决这个问题的方法。以下表达式
params.detail = params.detail?新字符串(params.detail.getBytes(8859_1),UTF8):null
正确编码unicode字符。
然而,使用这种方法是很乏味的,因为我必须对我的应用程序中的所有文本输入执行此操作。为什么unicode字符没有被grails和/或tomcat正确编码?我认为我有正确的设置。
如果您不使用Mysql,但默认发货的HSqlDB,没有看到
你的编码问题。由于使用了Mysql连接并已经设置了
useUnicode = true& characterEncoding = UTF-8
添加到MySql连接URL
您仍然需要为InnoDB和UTF-8添加特殊的hibernate方言:
Datasource.groovy
应该包含:
<$ p $
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;
/ **
*将默认字符集设置为UTF-8。
* /
public class MySQLUTF8InnoDBDialect extends MySQLInnoDBDialect {
$ b $ @Override
public String getTableTypeString(){
returnENGINE = InnoDB DEFAULT CHARSET = utf8 ;
$ b $ p $确保你的 Config。 groovy
有: grails.views.default.codec =html
grails .views.gsp.encoding =UTF-8
grails.converters.encoding =UTF-8
您的views / layouts / main.gsp开始于:
<%@ page contentType = text / html; charset = UTF-8%>
问候,
Jan
In my grails app, the unicode characters are not being encoded properly.
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.
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
will correctly encode the unicode character.
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.
解决方案 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.
Since you are using a Mysql Connection and already setting
useUnicode=true&characterEncoding=UTF-8
to the MySql connection URL
you still have to add a special hibernate dialect for InnoDB and UTF-8:
Datasource.groovy
should 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 your Config.groovy
has:
grails.views.default.codec = "html"
grails.views.gsp.encoding = "UTF-8"
grails.converters.encoding = "UTF-8"
And your views/layouts/main.gsp starts with:
<%@ page contentType="text/html;charset=UTF-8" %>
Greets,
Jan
这篇关于Grails不能正确编码unicode字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!