SpringWeb + Hibernate + Database encoding problems – solved!

Some time ago, I was investigating SmartClient (SmartGWT) and writting small app to connect via hibernate to MySQL database. During this tests I stuck with Polish locale problem. Such problems I’ve been facing not only this time, but many times in my career. Today, though it’s pretty easy, I will describe once for all how to solve the problem with web apps and Polish (but also any non english) encoding.

The first thing to be done is to set UTF-8 encodings in all web frontend files (*.html/*.jsp etc) by adding following code into tags:

Ok, this was easy and has nothing to do with database, but at least it makes sure, that localized characters will be displayed in proper way in the browser.

Second thing to do is to write filter which will set UTF-8 coding to requests and responses. It could look this:

public class ToukEncodingFilter extends OncePerRequestFilter {

    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws ServletException, IOException {

        response.setCharacterEncoding("UTF-8");

        request.setCharacterEncoding("UTF-8");
        chain.doFilter(request, response);

    }

}

 

Now, to make this filter running, the following lines have to be add to web.xml file:

CharacterEncodingFilter 
pl.touk.filters.ToukEncodingFilter 
CharacterEncodingFilter 
/*

Ok. So we have now all in web layer prepared. Now, to make it work with DB, the hibernate connection have to be configured like this:

url=jdbc:mysql:///?useUnicode=true&characterEncoding=UTF-8

It can be also done via hibernate properties.

Of course I assume, that database has UTF-8 encoding. I won’t write about this, becouse it is pretty well written all over internet. Just a little tip: it is easier to set this before creating any table ;-)

Last, but the most important thing, is to set jvm encoding parameters to UTF-8 and desired locale. If You are using Tomcat It can be done by modifying CATALINA_OPTS variable (in init script) by adding following options:

-Dfile.encoding=UTF-8 -Duser.country=PL -Duser.language=pl

That’s all!

You May Also Like

Simple trick to DRY your Grails controller

Grails controllers are not very DRY. It's easy to find duplicated code fragments in default generated controller. Take a look at code sample below. It is duplicated four times in show, edit, update and delete actions:

class BookController {
def show() {
def bookInstance = Book.get(params.id)
if (!bookInstance) {
flash.message = message(code: 'default.not.found.message', args: [message(code: 'book.label', default: 'Book'), params.id])
redirect(action: "list")
return
}
[bookInstance: bookInstance]
}
}

Why is it duplicated?

There is a reason for that duplication, though. If you move this snippet to a method, it can redirect to "list" action, but it can't prevent controller from further execution. After you call redirect, response status changes to 302, but after method exits, controller still runs subsequent code.

Solution

At TouK we've implemented a simple trick to resolve that situation:

  1. wrap everything with a simple withStoppingOnRender method,
  2. whenever you want to render or redirect AND stop controller execution - throw EndRenderingException.

We call it Big Return - return from a method and return from a controller at once. Here is how it works:

class BookController {
def show(Long id) {
withStoppingOnRender {
Book bookInstance = Book.get(id)
validateInstanceExists(bookInstance)
[bookInstance: bookInstance]
}
}

protected Object withStoppingOnRender(Closure closure) {
try {
return closure.call()
} catch (EndRenderingException e) {}
}

private void validateInstanceExists(Book instance) {
if (!instance) {
flash.message = message(code: 'default.not.found.message', args: [message(code: 'book.label', default: 'Book'), params.id])
redirect(action: "list")
throw new EndRenderingException()
}
}
}

class EndRenderingException extends RuntimeException {}

Example usage

For simple CRUD controllers, you can use this solution and create some BaseController class for your controllers. We use withStoppingOnRender in every controller so code doesn't look like a spaghetti, we follow DRY principle and code is self-documented. Win-win-win! Here is a more complex example:

class DealerController {
@Transactional
def update() {
withStoppingOnRender {
Dealer dealerInstance = Dealer.get(params.id)
validateInstanceExists(dealerInstance)
validateAccountInExternalService(dealerInstance)
checkIfInstanceWasConcurrentlyModified(dealerInstance, params.version)
dealerInstance.properties = params
saveUpdatedInstance(dealerInstance)
redirectToAfterUpdate(dealerInstance)
}
}
}