4

I'm receiving form data in my Spring MVC controller, but when I try to input non-ASCII characters I receive rubbish, áéíóú gets converted into áéíóú.

I'm using <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> in the jsp pages, Tomcat is configured to accept UTF-8 in the URI/Connection part and the form is set to acceptCharset="UTF-8". No idea where to look into next.

I'm testing on Firefox 38.0 in Ubuntu 14.04. The server is in Ubuntu 14.04 too.

Pedro Montoto García
  • 1,672
  • 2
  • 18
  • 38

2 Answers2

2

You need to add an encoding filter to your web.xml to make it encode the chars correctly:

<filter>
    <filter-name>encoding-filter</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>encoding-filter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>
FuRioN
  • 623
  • 5
  • 12
1

This is happening because the UTF-8 text is being read as latin1 encoding, which uses only one byte per char (for example, á (hex value C3 A1) is read as two latin1 chars á (hex values C3 and A1)).

You can check the HTML output if it has the <meta> tag on the <head> section:

<meta charset="utf-8" /> (if you´re using HTML5 DOCTYPE).

Niloct
  • 9,491
  • 3
  • 44
  • 57