NKS*_*NKS 5 hibernate utf-8 character-encoding jsf-2 applicationcontext
我们要为使用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>
Run Code Online (Sandbox Code Playgroud)
在运行应用程序时,我们遇到了异常
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?
Run Code Online (Sandbox Code Playgroud)
我们也尝试过使用以下内容但出现错误
<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
Run Code Online (Sandbox Code Playgroud)
经过一些努力后,我能够处理问题 - 以下是为我启用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&characterEncoding=UTF-8" />
<property name="maxPoolSize" value="10" />
<property name="maxStatements" value="0" />
<property name="minPoolSize" value="5" />
</bean>
Run Code Online (Sandbox Code Playgroud)
使用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>
Run Code Online (Sandbox Code Playgroud)
在过滤器中也指定请求编码格式
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 {
}
}
Run Code Online (Sandbox Code Playgroud)
现在即使您的应用程序和数据库不支持特殊字符检查数据库字符集,请尝试重新创建数据库并使用charset UTF8而不是latin1
归档时间: |
|
查看次数: |
6788 次 |
最近记录: |