سؤال

I'm having some trouble editing a table using <p:rowEditor>

I use encoding='windows-1252' to be able to use Swedish characters (å, ä, ö). Creating an entity works fine but when I edit it in a <p:dataTable> using <p:cellEditor> it commits characters that are unexpected. (If i enter "åäö" and save the edit (using p:celleditor), the table in the database contains "åäö").

My xhtml page starts like this:

<?xml version='1.0' encoding='windows-1252' ?> 
<!DOCTYPE html>
<html...

I've tried using a Character Encoding Filter:

public class CharacterEncodingFilter implements Filter {

    private static String ENCODING = "windows-1252";

    @Override
    public void destroy() {
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        request.setCharacterEncoding(ENCODING);
        response.setCharacterEncoding(ENCODING);
        chain.doFilter(request, response);
    }

    @Override
    public void init(FilterConfig config) throws ServletException {
    }
}

But to no avail. Any ideas why incell editing posts using a different character encoding?

Using:

  • NetBeans 7.0.1
  • Glassfish 3.1
  • Primefaces 3.0.M4
هل كانت مفيدة؟

المحلول

If i enter "åäö" and save the edit (using p:celleditor), the table in the database contains "åäö"

That's a typical CP1252 representation of UTF-8 encoded characters. In UTF-8, those characters are represented by the following bytes:

If you lookup those six individual bytes in the CP1252 codepage layout, you'll see that they represent exactly those characters åäö.

JSF/Facelets defaults to UTF-8 character encoding when it comes to generating the HTML response and processing the POST requests. That XML encoding attribute really doesn't change anything to that. It merely tells the XML parser which character encoding you've written/saved the file in so that it can parse it with the proper character encoding.

Changing the character encoding of the HTML response and processing of POST requests is to be done by setting the encoding attribute of the view root tag <f:view>:

<f:view encoding="CP1252">

But, I strongly disrecommend this. This way you're falling back from an universal character encoding which supports over a million characters to a proprietary character encoding which supports no more than 255 characters and is understood by Windows platforms only. Webpage visitors which use Mac/Linux or any other operating system will not be able to properly interpret the HTML response in that character encoding, nor will it be able to send the data back in that character encoding. Your web application will not be ready for successful world domination.

You need to solve this problem differently. It's apparently your database which needs to be changed to support UTF-8. How exactly to do this cannot be answered, because you didn't tell anything about the DB make/version. But changing the DB's/table's character encoding should be a matter of some SQL commands. Or if you're experimenting, just by truncating the DB and recreating it with the proper charset. Refer the DB-specific SQL manual for details.

See also:

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top