当我将 Mojarra 从 2.2.1 升级到 2.2.3(JBoss Wildfly Alpha 到 Beta)时,问题就开始了。
当我尝试提交带有特殊字符(波兰字母)的表单 (POST) 时,它们未正确 UTF-8 编码。
我做了什么?
写了一个过滤器
@WebFilter(urlPatterns = "/*", initParams = { @WebInitParam(name = "ignore", value = "true" ), @WebInitParam(name = "encoding", value = "UTF-8") }) public class CharacterEncodingFilter implements Filter { private String encoding = null; private FilterConfig filterConfig; // Should a character encoding specified by client be ignored private boolean ignore = true; @Override 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")); } @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { if ((ignore || (request.getCharacterEncoding() == null)) && (encoding != null)) { request.setCharacterEncoding(encoding); response.setCharacterEncoding(encoding); } chain.doFilter(request, response); } @Override public void destroy() { this.encoding = null; this.filterConfig = null; } }
每个 XHTML 都包含一行
<?xml version="1.0" encoding="UTF-8"?>
布局还包含有关编码的信息
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
向standalone.xml 添加了属性
<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"/> <property name="file.encoding" value="UTF-8"/>
从过滤器调试请求参数时登录控制台
index_form:people: Tischner PrzemysÅaw
index_form:j_idt66: index_form:j_idt66
index_form: index_form
index_form:dbId: 2881850
javax.faces.ViewState: 2748560203387116963:2575775533048879716
在浏览器中请求预览
我如何初始化 JSF 页面
<f:metadata> <f:viewParam name="name" value="#{followNewView.name}" /> <f:viewParam name="company" value="#{followNewView.company}" /> <f:viewParam name="companyURL" value="#{followNewView.companyURL}" /> <f:viewAction action="#{followNewView.init}" /> </f:metadata>
最后我仍然以不正确的编码结束: