织物(Tapestry):字符编码问题

3
我有一个 Tapestry 应用程序,它从表单中检索数据,将其写入数据库,然后显示结果。只要不使用特殊字符(如 Ä、Ö、Ü、ß、€ ...),一切都运行良好。
例如,文本:
TestäöüßÄÖÜ€$ 

会产生

TestäöüÃÃÃÃâ¬$

我猜这个问题与字符编码设置有关。 Tapestry Java 类:
@Component(parameters = {"clientValidation=false"})
private Form form;

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

然后我检查了POST请求的头部,注意到两件事:

  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.

我也尝试了以下内容:

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

并且

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

但是两者都没有成功(我寻找的是一般性解决方案而不是解决方法)。
有趣的是,this proposal 对于一些特殊字符(如ä、ö、ü、ß等)有效,但我不想使用ISO-8859-1。如何设置Tapestry在表单中使用的编码为UTF-8?我错过了什么或者我的问题完全不同的原因? 编辑:我进行了一项没有数据库的测试,问题仍然存在,所以这不是关于数据库端错误编码设置的问题。

你正在使用哪个版本的Tapestry? - Mark
1
这很可能不是Tapestry的问题,编码通常是UTF-8并且能够正常工作。当您使用调试器时,字符串首次中断的时间是在什么时候?(我曾经遇到过一个由于配置错误的Tomcat实例引起的问题。) - Henning
1
我正在使用Tapestry 5.2.5和Jetty 6.1.14,但是在我们的Tomcat部署上问题是相同的。我创建了一个没有数据库的示例页面,它只是将文本字段中的值写入DTO类。我在onValidate方法中设置了断点,但字符串已经被破坏了。是否有必要深入调试Tapestry内部,并且我需要查看哪里?在Tomcat中可能配置错误的设置是什么? - martin
1
它在Jetty 6.1.24上运行良好。可能是由于这个原因:http://blog.datentyp.org/index.php?/archives/101-jetty-ISO-8859-1-UTF-8.html。我现在会仔细检查我的Tomcat配置。 - martin
2个回答

3
确实是我的服务器配置错误。以下对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>

对我来说这并没有帮助,但是我使用了 JS 函数 encodeURIComponent() 而不是 escape(),现在一切都正常了 :) - MartinC

0
我在Tapestry 5.2.6和5.3.0-SNAPSHOT中尝试了你的测试字符串,它能够正常工作。我可以将它持久化到HSQLDB数据库,并按预期取回。
你使用的是哪个版本?如果你回到了5.0.x,你可能需要像这样做:http://wiki.apache.org/tapestry/Tapestry5Utf8Encoding

那只是在早期测试版(我想是5.0.8)中存在的问题,所有最终发行版本都可以正常运行。 - Henning

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接