I have an encoding problem which is driving me crazy. My web support both language english and spanish. Some of my tables (generated by hibernate) have as collation utf8_general_ci, some others, and I don't know why, have latin1_swedish_ci. But which is shaking me is that when people use my Contacts form and put inside for example a word with a "ñ" my Spring Controller takes it and send me an email which is Ok (I mean it is having the ñ) before save the data on MySQL. But when I check which is saved in MySQL (and my Contacts table have a utf8_general_ci collation) inside appears some horrible symbols replacing the "ñ" character, like Ãlvaro Núñez Cabeza de Váca. Resuming, jsp pages have UTF-8 declared, table is utf8_general_ci, hbn have utf-8 declared too:
# hibernate props
hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.show.sql=true
hibernate.hbm2ddl.auto=update
hibernate.format_sql=true
# hibernate props added to fix 4bytes encoded characters
hibernate.connection.CharSet=utf8mb4
hibernate.connection.characterEncoding=utf8
hibernate.connection.useUnicode=true
But all together is not working as expected.
Any help will be very welcomed.
SOLUTION: At least for me the only working fix was to add a filter in my web.xml. I am pretty sure there are better ways to solve encoding problems in a more elegant way, but in my case everything was configured to use UTF-8 and parto of my forms works well but others show Álvar Núñez Cabeza de Vaca as Ãlvar Núñez Cabeza de Vaca. The filter is:
<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>
As it is in this post Spring MVC UTF-8 Encoding