Tapestry: character encoding issue

2020-04-11 02:55发布

I have a Tapestry application, that retrieves data from a form, writes it to a database and then displays the result. It all works well as long as no special characters (Ä, Ö, Ü, ß, € ...) are used.

E.g. the text

TestäöüßÄÖÜ€$ 

will result in

TestäöüÃÃÃÃâ¬$

I guess the problem has something to do with a wrong character encoding setting.

Tapestry java class:

@Component(parameters = {"clientValidation=false"})
private Form form;

@Component(parameters = {"value=someDTO.name"})
private TextField someNameField;

Tapestry Template:

<t:form t:id="form">
    ...
    <t:textfield t:id="someNameField"/>
    ...
</t:form>

I checked my encoding settings at several places:

  1. HTML source:
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  1. Tapestry settings (this should be the default anyway):

    tapestry.charset=UTF-8
    
  2. Firefox says (Tools>Page Info: Encoding): UTF-8.

  3. The underlying database (Oracle) also uses UTF-8:

    character_set_system    utf8
    

Then I examined the header of the POST request and two things caught my eye:

  1. There is no content type specified in the header. I would expect something like this:

    Content-Type: application/x-www-form-urlencoded; charset=UTF-8
    
  2. Spaces are encoded with + instead of %20.

I also tried the following:

@Component(parameters = {"clientValidation=false", "enctype='application/x-www-form-urlencoded; charset=UTF-8'"})
private Form form;

and

@Component(parameters = {"clientValidation=false", "enctype='application/x-www-form-urlencoded; charset=UTF-8'", "accept-charset='utf-8'"})
private Form form;

but both with no success (and I'm looking for a general solution not a workaround).

Interestingly this proposal works for some special chars (like ä, ö, ü, ß and so on), but I do not want to use ISO-8859-1. How can I set the encoding Tapestry uses for forms to UTF-8? What am I missing or is there a completely different reason for my problem?

EDIT: I made a test without the database and the problem remains, so it's not about a wrong encoding setting on the db-side.

2条回答
老娘就宠你
2楼-- · 2020-04-11 03:23

It was indeed a misconfiguration of my server. The following addition to my web.xml solved it (of course this should also work with a non-Spring filter).

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

<filter-mapping>
    <filter-name>charsetFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>
查看更多
闹够了就滚
3楼-- · 2020-04-11 03:37

I have tried your test string in Tapestry 5.2.6 and 5.3.0-SNAPSHOT and it worked fine. I was able to persist it to the HSQLDB database and pull it back out as expected.

What version are you using? If you are back in 5.0.x you may need to do something like this: http://wiki.apache.org/tapestry/Tapestry5Utf8Encoding

查看更多
登录 后发表回答