gpt4 book ai didi

hibernate - 在JSF2.0、Hibernate、MySQL中启用UTF-8字符集

转载 作者:行者123 更新时间:2023-12-02 23:01:26 25 4
gpt4 key购买 nike

我们将为使用 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

最佳答案

经过一些解决之后,我能够处理这个问题 - 以下是我可以使用 UTF8 启用 JDBC 的代码

<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&characterEncoding=UTF-8& 结合使用即可达到目的

为了能够在 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

关于hibernate - 在JSF2.0、Hibernate、MySQL中启用UTF-8字符集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21037154/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com