我们正在使用 GWT 2.4、Errai 1.3.2 开发一个 Web 应用程序。它在 Tomcat 6 (6.0.35) 上运行,由 Maven (3.0.4) 构建。
在 Tomcat 上运行此应用程序时,特殊情况的传输不起作用。更具体地说,请求工作正常,但特殊字符的响应将它们转换为�。当使用 errai maven 原型时,它具有相同的行为。当使用 GWT-RPC 而不是 erai RPC 时,一切正常。在开发模式下运行相同的应用程序,问题不会发生。
在 chrome 中查看请求/响应时,两者都具有 UTF-8 字符编码。
我认为这可能是一个错误,因为在发送响应之前,错误中有一些字符串编码。
如果有人可以帮助我,那就太好了!这真是一个棘手的问题...
谢谢,沃尔特
PS:我已经尝试了以下可能的解决方案,但都不起作用:
设置 index.html 头:
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
定义自定义 Servlet 过滤器
WEB.xml
<filter>
<filter-name>SessionFilter</filter-name>
<filter-class>at.apa.excelsa.web.server.SessionFilter</filter-class>
<init-param>
<param-name>requestEncoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>SessionFilter</filter-name>
<url-pattern>*</url-pattern>
</filter-mapping>
过滤器.java
public class SessionFilter implements Filter {
String encoding;
@Override
public void init(FilterConfig filterConfig) throws ServletException {
encoding = filterConfig.getInitParameter("requestEncoding");
if (encoding == null) {
encoding = "UTF-8";
}
}
@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
if(request.getCharacterEncoding()==null) {
request.setCharacterEncoding(encoding);
}
response.setContentType("text/html; charset=UTF-8");
response.setCharacterEncoding("UTF-8");
chain.doFilter(request, response);
}
在 Tomcat Server.xml 上设置 URIEncoding
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" useBodyEncodingForURI="true" URIEncoding="UTF-8" />
pom.xml 中的 Maven
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
...
<build>
<outputDirectory>war/WEB-INF/classes</outputDirectory>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>gwt-maven-plugin</artifactId>
<version>${gwt.maven}</version>
<configuration>
...
<extraJvmArgs>-Xmx512m **-Dfile.encoding=UTF-8**</extraJvmArgs>
...
</configuration>
<executions>
<execution>
<goals>
<goal>resources</goal>
<goal>compile</goal>
</goals>
</execution>
</executions>
</plugin>
...