本文介绍了Tapestry:字符编码问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Tapestry应用程序,从表单中检索数据,将其写入数据库,然后显示结果。只要没有使用特殊字符(Ä,Ö,Ü,ß,...),它都可以正常工作。



例如文本

 TestäöüßÄÖÜ€$ 

将会导致

 TestäöüÃÃÃ¥



我猜这个问题与错误的字符编码设置有关。



Tapestry java class:

  @Component(parameters = {clientValidation = false})
私人表格;

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

Tapestry模板:

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

我在几个地方检查了我的编码设置:


  1. HTML来源:



 < meta http-equiv =Content-Typecontent =text / html; charset = utf-8> 




  1. Tapestry设置(这应该是默认的)

      tapestry.charset = UTF-8 


  2. Firefox说(工具>页面信息:编码):UTF-8。


  3. 底层数据库Oracle)也使用UTF-8:

      character_set_system utf8 


然后我检查了POST请求的标题,有两件事情引起了我的注意:


  1. 标题中没有指定内容类型。我会期望这样:

      Content-Type:application / x-www-form-urlencoded; charset = UTF-8 


  2. 空格以+而不是%20编码。 >


我也尝试过以下操作:

  @Component(parameters = {clientValidation = false,enctype ='application / x-www-form-urlencoded; charset = UTF-8'})
私人表格;

私人表格; 

但两个都没有成功(我正在寻找一个一般的解决方案,而不是解决方法)。



有趣的是,适用于一些特殊字符(如ä,ö,ü,ß等),但我不想使用ISO-8859-1。如何设置Tapestry用于窗体的编码为UTF-8?

EDIT :我做了一个没有数据库的测试,问题仍然存在。 ,所以它不是关于一个错误的编码设置在db端。

解决方案

这确实是我的服务器配置错误。下面的添加到我的web.xml解决了它(当然这也应该与非Spring过滤器工作)。

 < 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>


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.

解决方案

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>

这篇关于Tapestry:字符编码问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 19:41