Corrupted characters are redrawn in the input after the form is submitted

I can usually write a Czech line in the form:

enter image description here

But after checking (as well as when sending the collected string to the database), the string is in a different encoding:

enter image description here

h:outputTexts (jméno, příjmení) are still displayed normally, h:inputTexts are not.

Where to look for a problem?

UPDATE: HTTP response headers:

enter image description here

DECISION:

  • create a filter with request.setCharacterEncoding("UTF-8") in Filter#doFilter()
  • check all xml to configure UTF-8
  • add <f:view contentType="text/html" encoding="UTF-8"/> to the main xhtml
  • add these lines to hibernate.cfg.xml:

    <property name="hibernate.connection.characterEncoding">utf8</property>

    <property name="hibernate.connection.useUnicode">true</property>

+7
source share
3 answers

Given these symptoms, UTF-8 data was re-mapped using ISO-8859-x encoding. č ( LATIN SMALL LETTER C WITH CARON (U + 010D) ) exist in UTF-8 bytes 0xC4 and 0x8D . According to ISO-8859-1 codepage layout, these bytes represent Ä and [nothing] characters, respectively, which is exactly what kind of re is.

This particular problem can have many causes. Since Facelets itself already uses UTF-8 by default to process the HTTP POST request parameters and to record the HTTP response, there should / should not be anything you need to fix / change in the Java / JSF side,

However, when you manually grab the request parameter before the JSF creates / restores the view (for example, in a custom filter), it may be too late for Facelets to set the correct character encoding of the request. You will need to add the following line to the custom filter before continuing the chain, or to a new filter that appears before the filter causing the problems:

 request.setCharacterEncoding("UTF-8"); 

Also, if you explicitly or implicitly change the default character encoding of Facelets, for example, <?xml version="1.0" charset="ISO-8859-1"?> Or <f:view encoding="ISO-8859-1"> then Facelets will use ISO-8859-1 instead. You need to replace it with UTF-8 or delete it altogether.

If this is not the case, then only the database side will be the main suspect. On this side, I see two possible reasons:

  • The DB table does not use UTF-8.
  • The JDBC driver does not use UTF-8.

How to solve this exactly depends on the database server used. Usually you need to specify the encoding during the CREATE the database table, but you can also change it with ALTER . As for the JDBC driver, this usually needs to be resolved by explicitly specifying charset as the parameter for the connection URL. For example, in the case of MySQL:

 jdbc:mysql://localhost:3306/db_name?useUnicode=yes&characterEncoding=UTF-8 

See also:

+4
source

Try this solution: http://ibnaziz.wordpress.com/2008/06/10/spring-utf-8-conversion-using-characterencodingfilter/ In my cases it helps (with Russian)

In web.xml add Spring character encoding filter:

 <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> 
+4
source

I had exactly the same problem with the verification form, and I decided to answer it with Sergey.

BUT your filter should be in the first position in your web.xml. Moving my filter from third to first position solved my problem.

Hope this helps.

(Primes 3.2, JSF 2.1.2 with Jboss 7.1)

0
source

All Articles