Configuring Grails App for UTF8 Character Encoding
Posted By : Varun Sharma | 17-Apr-2013
Today I'm going to share my findings on how to use UTF-8 character encoding in grails. I had a requirement in one of my projects to use character encoding in my grails application. For that I have made several changes and these are explained below.
1) Changes in DataSource.groovy
url = "jdbc:mysql://localhost:3306/app_name?useUnicode=yes&characterEncoding=UTF-8" dialect = "com.domain.mysql.dialect.MySQLUTF8InnoDBDialect" For ex:- development { dataSource { dbCreate = "update" // one of 'create', 'create-drop','update' url = "jdbc:mysql://localhost:3306/app_name?useUnicode=yes&characterEncoding=UTF-8" dialect = "com.domain.mysql.dialect.MySQLUTF8InnoDBDialect" } }
2) 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"; } }
3) Changes in config.groovy
grails.views.default.codec = "html" // none, html, base64 grails.views.gsp.encoding = "UTF-8" grails.converters.encoding = "UTF-8"
4) Changes in gsp (I have added this in main.gsp only)
<%@ page contentType="text/html;charset=UTF-8" %>
5) Alter tables with query
ALTER TABLE table_name CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;
6) Add in server.xml (apache/conf)
Note:- May work without this
URIEncoding="UTF-8" useBodyEncodingForURI="true"
>Hope it helps !
Varun Sharma
[email protected]
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Varun Sharma
Varun is an experienced Groovy and Grails developer and has worked extensively on designing and developing applications with FaceBook , Linkedin and Twitter integrations using Grails technologies. Varun loves painting and photography.