Fixing Velocity's character encoding problems

I quite like writing quick web applications using Spring and Velocity. I've often been annoyed by the fact that I quite often get character encoding problems:

Encoding problem

Today I finally got around to looking into this and fixing it. It turns out that Velocity defaults to using the ISO-8859-1 encoding. Using Spring's VelocityViewResolver this can be changed:


	<bean id="viewResolver" class="org.springframework.web.servlet.view.velocity.VelocityViewResolver">
		<property name="cache" value="true"/>
		<property name="prefix" value=""/>
		<property name="suffix" value=".vm"/>
		<property name="contentType" value="text/html; charset=UTF-8"/>
	</bean>

With that change characters display as expected:
Problems Fixed

As usual: if I'd known how easy it was I'd have done it sooner…

2 thoughts on “Fixing Velocity's character encoding problems

  1. This is awesome. Helped me a lot. This is how you would do it in spring 3:

  2. I’m trying to do the same thing, i have a wepapp using velocity and spring, and we’re trying to i18n it to israel.
    we used the resourceTool in order to switch the context, but something’s getting in the way and the data doesn’t get in UTF-8 (tho before i changed it decoded well with unicode).
    any thoughts / ideas as to why?

Leave a Reply

Your email address will not be published. Required fields are marked *