【问题标题】:Enabling UTF-8 character set in JSF2.0, Hibernate, MySQL在 JSF2.0、Hibernate、MySQL 中启用 UTF-8 字符集
【发布时间】:2014-01-10 05:57:23
【问题描述】:

我们将为使用 JSF2.0、Hibernate、MySQL 设计的 Web 应用程序启用 UTF-8 字符编码。

以下是我们应用上下文文件中定义的数据源

<bean id="DataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
        <property name="driverClass" value="com.mysql.jdbc.Driver" />   
        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/dbname" />  
        <property name="maxPoolSize" value="10" />
        <property name="maxStatements" value="0" />
        <property name="minPoolSize" value="5" />
        <property name="useUnicode" value="yes" />
        <property name="characterEncoding" value="UTF-8" />
    </bean>

在运行应用程序时出现异常

Error creating bean with name 'SessionFactory' defined in ServletContext resource [/WEB-INF/classes/applicationContext.xml]: Cannot resolve reference to bean 'DataSource'  Error creating bean with name 'DataSource' defined in ServletContext resource [/WEB-INF/classes/applicationContext.xml]: Invalid property 'useUnicode' of bean class [com.mchange.v2.c3p0.ComboPooledDataSource]: Bean property 'useUnicode' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?

我们也尝试过使用以下方法,但出现错误

<bean id="DataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
        <property name="driverClass" value="com.mysql.jdbc.Driver" />   
        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/dbname?useUnicode=yes&characterEncoding=UTF-8;" />  
        <property name="maxPoolSize" value="10" />
        <property name="maxStatements" value="0" />
        <property name="minPoolSize" value="5" />
    </bean>


Error: The reference to entity "characterEncoding" must end with the ';' delimiter

【问题讨论】:

    标签: hibernate jsf-2 utf-8 character-encoding applicationcontext


    【解决方案1】:

    经过一些工作后,我能够处理该问题 - 以下是我的代码,可以让 JDBC 与 UTF8 一起使用

    <bean id="DataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
            <property name="driverClass" value="com.mysql.jdbc.Driver" />   
            <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/dbname?useUnicode=true&amp;characterEncoding=UTF-8" />  
            <property name="maxPoolSize" value="10" />
            <property name="maxStatements" value="0" />
            <property name="minPoolSize" value="5" />
        </bean>
    

    使用 useUnicode=true&amp;amp;characterEncoding=UTF-8&amp;amp; 达到目的

    为了能够与 Hibernate 一起使用,还需要在 hibernate 属性中指定以下内容

    <property name="hibernateProperties">
                <props>
                    <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                    <prop key="hibernate.show_sql">false</prop>
                    <prop key="hibernate.connection.useUnicode">true</prop>
                    <prop key="hibernate.connection.characterEncoding">UTF-8</prop>
                    <prop key="hibernate.connection.charSet">UTF-8</prop>               
                </props>
            </property> 
    

    在过滤器中也指定请求编码格式

    public class ApplicationFilter implements Filter {    
            public void destroy() {
            }
    
        public void doFilter(ServletRequest request, ServletResponse response,
                    FilterChain chain) throws IOException, ServletException {
                request.setCharacterEncoding("utf-8");
            }
    
            public void init(FilterConfig fConfig) throws ServletException {
            }
        }
    

    现在,即使您的应用程序和数据库不支持特殊字符,请检查数据库字符集,尝试重新创建数据库并使用字符集 UTF8 代替 latin1

    【讨论】:

    【解决方案2】:

    当您使用 UI 安装程序安装 MySQL 时,您可以将默认字符集设置为“UTF-8”。之后一切正常。我总是在安装时设置它,所以我不知道以后如何设置它。但如果您不想重新安装 MySQL,请查看此链接:

    How do i Setup utf-8 as standard character set for a mysql server?

    【讨论】:

      猜你喜欢
      • 2011-02-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-14
      • 2012-01-14
      • 2013-05-26
      • 1970-01-01
      相关资源
      最近更新 更多