goldstift's 2 cent on technology and software development


Broken special chars in Spring Roo?

This is the first post in an upcoming series of posts on Spring Roo (see footer for details an Roo).

Problem: Special chars and Umlauts are not correctly displayed in my Roo-Application

[german version]
There are often questions and support requests on the above named topic in Spring Roo-Forum. Mostly questioners write that german "Umlauts" like ä, ö, ü and ß or other special chars in cyrillic are not correctly display or saved to the database. With the following post I'd like to review the most common sources of the problem and come up with solutions.

Encoding of the HTML/ JSP page

Roo generated JSPs are always delivered with UTF-8 encoding. In case you write your additional JSPs by hand, you should assure these pages are also delivered to the browser in UTF-8 encoding so the form data will be correctly send on submit.
This can be achieved by specifying the following line of code at the beginning of each JSP:
< contenttype="text/html;charset=UTF-8"

To show you an example where to exactly put this, the "default.jspx", which serves a frame for all other JSP pages via Apache Tiles, contains the following lines to preserve UTF-8 encoding in the resulting HTML:
<html xmlns:jsp="" xmlns:c="" xmlns:tiles="" xmlns:spring="" xmlns:util="urn:jsptagdir:/WEB-INF/tags/util" >  

<jsp:output doctype-root-element="HTML" doctype-system="about:legacy-compat" />

< contentType="text/html;charset=UTF-8" />

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

<!-- ... -->

To check whether your page is delivered in UTF-8 encoding, open it in your browser and have a look at menu "View > Character Encoding".

CharacterEncodingFilter in web.xml file

This is the most common point of failure. We're talking about the order in which the <filter-mapping>-Tags occur in your "src/main/webapp/WEB-INF/web.xml". These tags define in which order the filters are executed on each HTTP-Request.
Roo generates the correct order since Version 1.1.1! Previous versions are still buggy.

The correct order looks like the following:
<filter-name>Spring OpenEntityManagerInViewFilter</filter-name>

In case you want to change the order it is important to understand at least a minimum of the inner workings of these filters:
To avoid problems with the encoding of your special chars, "CharacterEncodingFilter" has to be run before any other filter has worked on the request data!
For this reason the general advice is: the <filter-mapping> for CharacterEncodingFilter has to be at the first position in your "web.xml" (See ROO-1698 for an explanation - the charset CANNOT be set to the request after the InputStream has been read)!

Database connection

The last point is to assure that your application communicates with the database via unicode with a default encoding of UTF-8.

As Roo generates the correct connection string for MySQL since "the early days" there haven't been many bugs with this lately.
The connection string can be configured in "src/main/resources/META-INF/spring/" and looks like the following for MySQL:


Since version 1.1.1 Spring Roo provides a (by now) bug free support for special chars and umlauts, which as been tested with various languages worldwide.

If you have any questions on Spring Roo or the topic named above you can find competent assistance in the Roo-Forum.

References/ Links used in the text

Spring Roo is a Code-Generation-Tool for Software development in Java. It is a lightweight developer tool that makes it fast and easy to deliver instant results. Best of all, you code 100% in Java and get to reuse all your existing Java knowledge, skills and experience.

There are already many Add-Ons available to customize Roo to your special needs. In case you don't find an Add-On that fits your needs, you can easily create your own Add-On in just a few steps.

My next Conferences