如何在一个主要的webapp中设置charset?

时间:2012-03-14 20:56:42

标签: java jsf-2 character-encoding primefaces

让我与主要观点3.1分享我独特的失望,直到现在...... 我正在使用RichFaces应用程序中的phew组件,当我意识到问题时,一切都很好,我的母语中的一些字符显示错误,甚至UTF-8字符集在所有地方都被声明,我知道它是必需的。

当在页面中输入“SãoPaulo”等特殊字符时,会出现问题。提交后的数据将重新显示为“São£Paulo”

我已经尝试了下面的解决方法:

1)Eclipse IDE:文本文件enconding选项

2)jsf文件:

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 

3)JBOSS server.xml:

<Connector protocol="HTTP/1.1" URIEncoding="UTF-8" port="${jboss.web.http.port}" address="0.0.0.0" 
         redirectPort="${jboss.web.https.port}" />

4)的web.xml:

<?xml version="1.0" encoding="UTF-8"?>

5)jsf文件:

<h:form acceptcharset="UTF-8" enctype="application/form-data">

6)将primefaces版本升级到3.2

感谢您的帮助! ; - )

2 个答案:

答案 0 :(得分:9)

我被迫创建了一个为每个请求设置字符集的过滤器......

public class CharacterEncodingFilter implements Filter {

    @Override
    public void destroy() {
        // TODO Auto-generated method stub
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response,
        FilterChain chain) throws IOException, ServletException {
        response.setCharacterEncoding("UTF-8");
        request.setCharacterEncoding("UTF-8");
        chain.doFilter(request, response);
    }

    @Override
    public void init(FilterConfig arg0) throws ServletException {
        // TODO Auto-generated method stub
    }

}

这解决了我的问题

答案 1 :(得分:1)

我只是取消注释conf / web.xml(Tomcat服务器web.xml)中过滤所有请求并转换为UTF-8的部分。

 <!-- A filter that sets character encoding that is used to decode URIs-->
 <!-- parameters in a POST request -->
 <filter>
        <filter-name>setCharacterEncodingFilter</filter-name>
        <filter-class>org.apache.catalina.filters.SetCharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
 </filter>

  <!-- The mapping for the Set Character Encoding Filter -->
  <filter-mapping>
        <filter-name>setCharacterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
  </filter-mapping>