1

I have problem with saving data from forms with Hibernate, basically encoding goes wrong, so weird characters are being saved in database and populated as consequence in form inputs. This is configuration I prepared so far:

in web.xml

 <filter>
    <filter-name>SetCharacterEncodingFilter</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>SetCharacterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

in applicationContext.xml (tried both patterns 'UTF-8' and 'utf8')

<property name="url" value="jdbc:postgresql://localhost:5432/mydb?characterEncoding=UTF-8" />

...

<property name="hibernateProperties">
            <props>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
                <prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQL9Dialect</prop>
                <prop key="hibernate.connection.CharSet">utf8</prop>
                <prop key="hibernate.connection.characterEncoding">utf8</prop>
                <prop key="hibernate.cache.use_query_cache">true</prop>
                <prop key="hibernate.cache.use_second_level_cache">true</prop>
                <prop key="hibernate.cache.region.factory_class">
                    org.hibernate.cache.ehcache.EhCacheRegionFactory
                </prop>
                <prop key="hibernate.connection.useUnicode">true</prop>
                <prop key="hibernate.temp.use_jdbc_metadata_defaults">false</prop>
                <prop key="show_sql">true</prop>

                <prop key="hibernate.c3p0.min_size">5</prop>
                <prop key="hibernate.c3p0.max_size">20</prop>
                <prop key="hibernate.c3p0.timeout">300</prop>
                <prop key="hibernate.c3p0.max_statements">50</prop>
                <prop key="hibernate.c3p0.idle_test_period">3000</prop>
            </props>
</property>

pom.xml

<properties>
        <spring.version>4.1.2.RELEASE</spring.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

database, jsp files and project have ut8 encoding as well, also I added URIEncoding="UTF-8" to my connector configuration in server.xml. What else do I need to add or what am I doing wrong?

Markownikow
  • 457
  • 1
  • 4
  • 14
  • What database are you using. Have you checked the encoding of the columns? – xiaofeng.li Nov 21 '16 at 23:33
  • I am using postgres 9, I am able to insert correct values with raw query and display them correctly in view later, problem occurs after I try to save something from application form, I am using @Valid annotation and BindingResult object as arguments of action responsible for saving data – Markownikow Nov 21 '16 at 23:59

2 Answers2

0

You can set a breakpoint and check if the encoding from bytes (UTF-8) to Unicode was done correctly. The next thing to do is check the collation of the database/table. When you create a database/table you specify collation, the equivalent of a charset (you typically select a default during installation), if this does not support the characters that you store you may get garbage out.

Klaus Groenbaek
  • 4,820
  • 2
  • 15
  • 30
  • data are broken already in controller, so when I debug/print value of entity in action before saving its already incorrect, same for request parameter - if I call getParameter("name") on HttpServletRequest object i get invalid encoded value, but I called setCharacterEncoding("UTF-8") and inside action i can see it took affect but again value of param is still wrong. – Markownikow Nov 23 '16 at 10:20
  • Are you posting form data or encoding data on the URL. Normally form data is posted in the same encoding as the page the form is on, otherwise you can use accept-charset to configure it. URL parameters should be URLEncoded, but since this is basically a hex encoding, you will need to tell the Servlet container how to process URIs. On Tomcat this is done in the definition in server.xml, using xml attribute URIEncoding="UTF-8". – Klaus Groenbaek Nov 23 '16 at 18:43
  • Thank you for your suggestions, I already tried accept-charset attribute (with jsp form tag and its acceptCharset="UTF-8) also the page has: <%@page pageEncoding="UTF-8"%> , adding URIEncoding in server.xml was one of the first thing I tried but unfortunately it doesnt solve problem – Markownikow Nov 23 '16 at 19:18
  • What servlet container are you using, and have you tried another. Otherwise I would recommend the traditional debugging path: Create the worlds simplest example (preferably on github so you can post a link), if the problem exist, we can help solve/debug it. – Klaus Groenbaek Nov 24 '16 at 09:12
  • It has been at least 10 years ago, since I had this type of issue with form posted data. The error is more common when data is part of the URL. Have you checked that the browser has detected the correct encoding of the page that you submit. In chrome View->Encoding=UTF-8. – Klaus Groenbaek Nov 24 '16 at 14:28
  • thanks for being involved, I have solved my problem. Apparently spring security filters run before encoding filter defined in my web.xml, so in security initializer i setup encoding again and it helps, see comment above – Markownikow Nov 24 '16 at 20:22
0

The solution is:

@Order(1)
public class SpringSecurityInitializer extends AbstractSecurityWebApplicationInitializer {

    @Override
    protected void beforeSpringSecurityFilterChain(ServletContext servletContext) {
        FilterRegistration.Dynamic characterEncodingFilter = servletContext.addFilter("encodingFilter", new CharacterEncodingFilter());
        characterEncodingFilter.setInitParameter("encoding", "UTF-8");
        characterEncodingFilter.setInitParameter("forceEncoding", "true");
        characterEncodingFilter.addMappingForUrlPatterns(null, false, "/*");
    }

}

based on comments from CharacterEncodingFilter don't work together with Spring Security 3.2.0

Community
  • 1
  • 1
Markownikow
  • 457
  • 1
  • 4
  • 14