1

I have the simplest possible JSF2 form inserting a table field in a MySQL db.

My facelet has <?xml version="1.0" encoding="UTF-8"?> at the top.

My input is a thing like <h:inputText value="#{testController.entity.test}" id="test" />

So I try to insert "provà" in my Jpa2/Hibernate entity. Then I persist it and, when I look for the saved value in my MySQL db....

I find "provÃ". It's the same old encoding problem I always solve by trying and trying....

Can you suggest me what to check first for fixing it?

I mean, I know there's a lack of info in this question, I just wanna see what do you ask me: in your questions I will find the answer! :)

Fabio B.
  • 9,138
  • 25
  • 105
  • 177
  • Shouldn't the encoding be codified in the h:form attribute? The xml is for the .xml file itself – SJuan76 Jun 14 '12 at 08:38

2 Answers2

1

My facelet has <?xml version="1.0" encoding="UTF-8"?> at the top.

This is irrelevant to the problem. This only tells the XML parser which charset to use when reading the XML file content before parsing it into a tree based hierarchy.

Your concrete problem can have 2 possible causes:

  1. The HTTP request character encoding is not set right. This is however not a normal situation as JSF/Facelets by default already uses UTF-8 throughout all layers. This problem can however occur whenever something else has accessed the HTTP request body before JSF/Facelets has set the proper character encoding. PrimeFaces 3.x is known to do that. See also Unicode input retrieved via PrimeFaces input components become corrupted. One of the ways to fix this is creating a filter which look like as in UTF-8 form submit in JSF is corrupting data.

  2. The JDBC connection character encoding is not set right. The MySQL JDBC driver uses the client platform default character encoding instead of the server table character encoding. Make sure that your MySQL JDBC URL look like as follows:

    jdbc:mysql://localhost:3306/db_name?useUnicode=yes&characterEncoding=UTF-8
    
Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • I tried again now with h:form acceptcharset="UTF-8"... nothing. I exclude opt.2 because I just logged the wrong input in the controller, no db access actually. Could PrettyFaces have anything to do with it? – Fabio B. Jun 14 '12 at 14:40
  • I have not suggested to use `acceptcharset` at all. This will indeed not fix your problem. It was someone else who made the incorrect suggestion. If the input is already invalid in the log, then it's cause #1. Just create that filter. – BalusC Jun 14 '12 at 14:51
  • It worked! But.... it's damn ugly! Why do I have to create a filter??? Maybe because I use PrimeFaces upload filter for file upload and PrettyFaces for url rewriting. How can I discover why, in your opinion? – Fabio B. Jun 14 '12 at 15:32
  • It's answered in the 1st link in my answer. – BalusC Jun 14 '12 at 15:39
0

Try putting h:form acceptcharset="UTF-8" in your xhtml(worked for me):

Rafael Roque
  • 377
  • 2
  • 5
  • 14