Thursday, 18 July 2013

Diacritics problem in Spring web app and solution

I had a diacritics problem in Spring Web application. The XML file is stored in UTF-8 and the XML parser read it properly with UTF-8 encoding - if I log the texts just read, it shows fine with diacritics in the log. However, when I display it in the web page, it shows some strange '?' which means the diacritics can not be passed properly.

Here is the solution:

1. Put encodingFilter as the first filter in web.xml:
    <filter>
        <filter-name>encodingFilter</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>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>

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

2. Put 'UTF-8' in both contentType and pageEncoding
<%@ page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"%>

It solved my problem!

No comments:

Post a Comment