views:

76

answers:

3

Hi there, I am using Spring 3 MVC and MySQL 5 as my DB.

I have created a form using Spring form tags for updating an object in my database.

I enter "Ticket price: €35" into one of this form's text input fields and save the item. Then when I view the item in a display page, the text appears as "Ticket price: ?35".

If I enter "Ticket price: €35" directly into the database using MySql Query Browser, then the euro symbol displays fine through the display page.

What do I need to do so that my Spring form saves the euro symbol correctly to the database?


Update:

Hi, I tried your suggestion, Bozho, with no luck.. my web.xml looks like this:

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




<servlet>
    <servlet-name>baseApp</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>baseApp</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>

Any further thoughts?

A: 

You have to make sure, that the encoding of your string containing the € is set to the correct encoding (same of your table). I would suggest to use UTF-8

Erik
-1. Internal representation of all `String` s in Java is UTF-16. What he need is a correct transformation chain - from the page encoding to UTF-16 (performed by `CharacterEncodingFilter`) and from UTF-16 to the database encoding (performed by JDBC driver).
axtavt
It does not matter, whether he sets the encoding of his string to UTF-16 first and then converts it to the database encoding by JDBC, or if he converts it to UTF-8 from the webpage and the JDBC does not have to to any further conversion or do I miss something?
Erik
@Erik - yes you have. Read @axtavt's answer again ... carefully. ALL Java Strings are UTF-16. Period.
Stephen C
I don't agree - sorry: you can set the encoding of strings in java to ANY encoding. I don't care if the deepest inner of java reencode it to UTF-16 and when I use the string it automatically reencodes it back to UTF-8 for me. But it IS possible to define the encoding of a string to UTF-8 and use it as such. See: java.nio.charset.CharsetEncoder
Erik
+2  A: 

Use the spring character encoding filter. In web.xml

<filter>
    <filter-name>CharacterEncodingFilter</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>

And map it to the default servlet

Then, your MySQL connection string should look like this:

jdbc:mysql://localhost/dbName?characterEncoding=utf8
Bozho
Hi there, thanks for the answer. I have tried adding this filter and unfortunately I'm still having the same issue, the euro symbol appears as a question mark. Any further thoughts?
Deiter Zaputo
If you still have the same issue, then your JDBC/DB thing is still not persisting the char as UTF-8. Try adding `useUnicode=yes` to connection string. MySQL has some oddness with regard to this all.
BalusC
Great, I've now made that change to my dataSource URL as well as the previous changes to web.xml as above. Now when I save the form with a € I get an exception: ERROR org.hibernate.util.JDBCExceptionReporter - Data truncation: Data truncated for column 'description' at row 1. 'description' is the field that I'm entering the € into and it has a MySQL data type of TEXT.Any thoughts on why I'm getting this exception?
Deiter Zaputo
perhaps your column doesn't have enough length?
Bozho
I'm not sure column length is the problem.. the column is of type TEXT and the only value i'm entering into it is €, i.e. just the single character... odd.
Deiter Zaputo
Thanks for the help.. I've started a new question for my data truncation error here:http://stackoverflow.com/questions/3230561/spring-3-mvc-mysql-cannot-store--character
Deiter Zaputo
A: 

(moved update into question, please delete this "answer")

Deiter Zaputo