在JSF2.0,Hibernate,MySQL中启用UTF-8字符集 [英] Enabling UTF-8 character set in JSF2.0, Hibernate, MySQL

查看:257
本文介绍了在JSF2.0,Hibernate,MySQL中启用UTF-8字符集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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

  ; bean id =DataSourceclass =com.mchange.v2.c3p0.ComboPooledDataSourcedestroy-method =close> 
< property name =driverClassvalue =com.mysql.jdbc.Driver/>
< property name =jdbcUrlvalue =jdbc:mysql:// localhost:3306 / dbname/>
< property name =maxPoolSizevalue =10/>
< property name =maxStatementsvalue =0/>
< property name =minPoolSizevalue =5/>
< property name =useUnicodevalue =yes/>
< property name =characterEncodingvalue =UTF-8/>
< / bean>

运行应用程序时遇到异常

 在ServletContext资源[/WEB-INF/classes/applicationContext.xml]中定义名称为SessionFactory的bean时出错:无法解析对DataSource的引用创建名为在ServletContext资源中定义的DataSource[/WEB-INF/classes/applicationContext.xml]:bean类的无效属性useUnicode[com.mchange.v2.c3p0.ComboPooledDataSource]:Bean属性'useUnicode'不可写,无效的setter方法。设置器的参数类型是否与getter的返回类型匹配? 

我们也尝试使用以下操作但会收到错误

 < bean id =DataSourceclass =com.mchange.v2.c3p0.ComboPooledDataSourcedestroy-method =close> 
< property name =driverClassvalue =com.mysql.jdbc.Driver/>
< property name =jdbcUrlvalue =jdbc:mysql:// localhost:3306 / dbname?useUnicode = yes& characterEncoding = UTF-8; />
< property name =maxPoolSizevalue =10/>
< property name =maxStatementsvalue =0/>
< property name =minPoolSizevalue =5/>
< / bean>


错误:对实体characterEncoding的引用必须以;分隔符结尾

$ b $经过一些工作后,我能够处理这个问题 - 下面是为我启用JDBC与UTF8工作的代码

$

b
$ b

 < bean id =DataSourceclass =com.mchange.v2.c3p0.ComboPooledDataSourcedestroy-method =close> 
< property name =driverClassvalue =com.mysql.jdbc.Driver/>
< property name =jdbcUrlvalue =jdbc:mysql:// localhost:3306 / dbname?useUnicode = true& amp; characterEncoding = UTF-8/>
< property name =maxPoolSizevalue =10/>
< property name =maxStatementsvalue =0/>
< property name =minPoolSizevalue =5/>
< / bean>

使用 useUnicode = true& amp; characterEncoding = UTF-8 & 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 {
}
}

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


We are to enable UTF-8 character encoding to our web application designed using JSF2.0, Hibernate, MySQL.

Following is the datasource defined in our application context file

<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>

While running the application we are getting exception

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?

We have also tried using the following but get an error

<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

解决方案

After some work around I am able to handle the issue- Following is the code that is working for me to enable JDBC working with 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>

Using useUnicode=true&amp;characterEncoding=UTF-8 with &amp; served the purpose

To be able to use the same with Hibernate also specify the following to the hibernate properties

<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> 

Specify request encoding format in the filter as well

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 {
        }
    }

Now even if your application and db are not supporting special characters check the database character set , try by recreating the database and using charset UTF8 instead latin1

这篇关于在JSF2.0,Hibernate,MySQL中启用UTF-8字符集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆