我今天花了很多时间来弄清楚如何在运行在JBoss上的JSF应用程序中(使用JBoss 7 Final)强制正确解码编码的字符。 当您有例如通过URL传递中文字符时,就会出现此问题。 假设您有指点事件,编码为%E6%8C%87%E4%BA%8B。 令人惊讶,但是这些字符以指事的形式到达服务器端。 服务器使用ISO-8859-1自动解码它们。 因此,如果您尝试像这样自己解码,则没关系:
FacesContext fc = FacesContext.getCurrentInstance();
String param = fc.getExternalContext().getRequestParameterMap().get(name);
String decodedParam = java.net.URLDecoder.decode(param, "UTF-8");
这无济于事,因为字符已经被错误地解码,并且您从请求参数映射中获得了已经被错误解码的字符。 如果您在页面上也没有帮助
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
要克服此错误,您需要做两件事:特殊的字符编码过滤器和JBoss的standalone.xml中的配置。 过滤器应同时为请求和响应设置配置的编码。
public class CharacterEncodingFilter implements Filter {
/** The default character encoding to set for request / response. */
private String encoding = null;
/** The filter configuration object. */
private FilterConfig filterConfig;
/** Should a character encoding specified by the client be ignored? */
private boolean ignore = true;
public void destroy() {
encoding = null;
filterConfig = null;
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException,
ServletException {
// conditionally select and set the character encoding to be used
if ((ignore || (request.getCharacterEncoding() == null)) && (encoding != null)) {
request.setCharacterEncoding(encoding);
response.setCharacterEncoding(encoding);
}
// pass control on to the next filter
chain.doFilter(request, response);
}
public void init(FilterConfig filterConfig) throws ServletException {
this.filterConfig = filterConfig;
this.encoding = filterConfig.getInitParameter("encoding");
String value = filterConfig.getInitParameter("ignore");
this.ignore = ((value == null) || value.equalsIgnoreCase("true") || value.equalsIgnoreCase("yes"));
}
}
注意:如果仅设置请求的编码,则无济于事。 您还应该通过response.setCharacterEncoding(encoding)将其设置为响应。 web.xml中的配置看起来像
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>xyz.mypackage.CharacterEncodingFilter</filter-class>
<init-param>
<description>override any encodings from client</description>
<param-name>ignore</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<description>the encoding to use</description>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>*.jsf</url-pattern>
</filter-mapping>
现在,您必须在关闭<extensions>标记之后直接将以下系统属性添加到standalone.xml中:
<system-properties>
<property name="org.apache.catalina.connector.URI_ENCODING" value="UTF-8"/>
<property name="org.apache.catalina.connector.USE_BODY_ENCODING_FOR_QUERY_STRING" value="true"/>
</system-properties>
从文档中:
- org.apache.catalina.connector.URI_ENCODING指定%xx解码URL后用于解码URI字节的字符编码。 如果未指定,将使用ISO-8859-1。
- org.apache.catalina.connector.USE_BODY_ENCODING_FOR_QUERY_STRING指定是否应将contentType中指定的编码用于URI查询参数,而不是使用org.apache.catalina.connector.URI_ENCODING。 出现此设置是为了与Tomcat 4.1.x兼容,其中在contentType中指定的编码或使用Request.setCharacterEncoding方法显式设置的编码也用于URL中的参数。 默认值为false。
现在,JBoss看起来为响应设置了字符编码,并使用它来解码URL参数。 希望此信息可以帮助您节省时间。