views:

179

answers:

2

Hi!

In my earlier post there was a problem with JSF charset handling, but also the other part of the problem was MySQL connection parameters for inserting data into db. The problem was solved.

But, I migrated the same application from JSP to facelets and the same problem happened again. Characters from input fields are replaced when inserting to database (č is replaced with Ä), but data inserted into db from sql scripts with proper charset are displayed correctly. I'm still using registered filter and page templates are used with head meta tag as following:

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-2">

If I insert into h:form tag the following attribute:

acceptcharset="iso-8859-2"

I get correct characters in Firefox, but not in IE7.

Is there anything else I should do to make it work?

Thanks in advance.

+1  A: 

Add the following line to the filter:

response.setContentType("text/html;charset=ISO-8859-2");

Don't use acceptcharset attribute. IE has serious bugs with it.

Also, when you're using a <?xml?> declaration in top of Facelets XHTML page, ensure that it's using the desired charset or just remove the whole declaration, it's not strictly required.

<?xml version="1.0" encoding="ISO-8859-2"?>
BalusC
Thank you for your reply.Unfortunately, it didn't solve my problem.I don't have any problems displaying correct characters. They are read correctly, but I can't write them to db as I should (and it's not db problem since I can write them correctly either through scripts or with acceptcharset attbitute set).
Vladimir
Check the response headers in the client side with e.g. firebug.
BalusC
I get the following response headers:Server: Apache-Coyote/1.1Content-Type: text/html;charset=UTF-8Transfer-Encoding: chunkedDate: Wed, 09 Jun 2010 12:39:37 GMT200 OKSo, charset is not as I set it, but I don't know why.
Vladimir
So it's been overriden with `UTF-8` after all. How did you declare your Facelets XHTML page? With `<?xml encoding="UTF-8" ?>`? Change or just remove this line.
BalusC
I left it out from the beginning.
Vladimir
Interesting. This requiers more tests here. Anyway, any reason that you don't want to use UTF-8? It's actually the best choice as of now.
BalusC
Well, I use already present MySql database beneath with latin2 encoding and I don't know how sorting is going to perform if I switch to another encoding.
Vladimir
I left out one piece of information which might be important, I'm using JSF 1.2.
Vladimir
After I added contentType="text/html; charset=ISO-8859-2" to f:view tag, characters were correctly handled in Firefox, but not in IE7. Response headers contained: Content-Type: text/html; charset=UTF-8;charset=ISO-8859-2.
Vladimir
Yes, *something* is implicitly adding `charset=UTF-8`, I question if it is Facelets. Are you using the latest Facelets version?
BalusC
I'm using JSF 1.2 and Facelets 1.1.15.
Vladimir
Unexpectedly the problem was solved by adding <?xml version="1.0" encoding="ISO-8859-2"?> to all xhtml files. Although I have done it once BalusC gave the answer, it didn't fix the problem at that moment. But, now it works fine and I don't know if something else I did meanwhile combined with xml declaration fixed it.BalusC, thank you for your effort!
Vladimir
Apparently you were hotdeploying, but the changed file wasn't reloaded into server's work memory correctly. An explicit server restart should have fixed this. You're welcome!
BalusC
A: 

i think you can see the implementation of org.springframework.web.filter.CharacterEncodingFilter and you can start your tomcat by adding -Dfile.encoding=ISO-8859-2 as jvm arguments

AshkaN
He's already doing the same as this filter does and the `-Dfile.encoding` doesn't have any influence on the charset to be used by the client (webbrowser) to encode the POST data in. The `Content-Type` header does, but *something* (Facelets?) is implicitly setting it to UTF-8.
BalusC
Thank you for your reply. Yes, I do have filter which worked for me before I moved to Facelets.I suppose it has to do something with Facelets, especially I modified my web pages from JSP to Facelets and that's when problems started.Also, if I remove line: <view-handler>com.sun.facelets.FaceletViewHandler</view-handler> from faces-config.xml, response headers contain Content-Type: application/xhtml+xml;charset=ISO-8859-2
Vladimir
I came across this post: http://forums.sun.com/thread.jspa?threadID=5421551I don't know if it is the answer to my question. Hope not.
Vladimir