Grails not encoding the unicode characters properly

后端 未结 2 1898
小蘑菇
小蘑菇 2020-12-06 13:50

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

相关标签:
2条回答
  • 2020-12-06 14:21

    Setting the Dialect to UTF-8 is not going to work. You have to also modify the tables or the columns of your table to support UTF-8.

    Here is the command to alter the table

    ALTER TABLE tbl_name CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;
    

    or you can try to just modify the column in which you want to store utf-8

    using the command

    ALTER TABLE t MODIFY col1 CHAR(50) CHARACTER SET utf8;
    
    0 讨论(0)
  • 2020-12-06 14:36

    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

    0 讨论(0)
提交回复
热议问题