HomeBlogAboutTools

Fixing Velocity's character encoding problems

uncategorized

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…