UTF-8, JSP and Spring MVC

If you are using UTF-8 with your Spring web app, then I would suggest using the following configurations.

In web.xml add:

<jsp-config>
    <jsp-property-group>
        <url-pattern>*.jsp</url-pattern>
        <page-encoding>UTF-8</page-encoding>
    </jsp-property-group>
</jsp-config>

(so that you don’t have to add <%@ page pageEncoding="utf-8" %> to every jsp file) and

<filter>
    <!-- This filter has to come before other filters. -->
    <filter-name>characterEncodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
        <param-name>encoding</param-name>
        <param-value>UTF-8</param-value>
    </init-param>
</filter>

<filter-mapping>
    <filter-name>characterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

And if you are using ResourceBundleMessageSource you need to set defaultEncoding in you Spring Servlet config:

<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
    <property name="basename" value="messages" />
    <property name="defaultEncoding" value="UTF-8" />
</bean>

Note that if you are not using JDK 1.6 or above you have to use ReloadableResourceBundleMessageSource instead of ResourceBundleMessageSource to be able to set defaultEncoding.

Now, if you remember to save all files using UTF-8, you should be fine.

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>