Including UTF-8 Character Set in JSF2.0, Hibernate, MySQL

We must include the UTF-8 character encoding in our web application developed using JSF2.0, Hibernate, MySQL.

Below is the data source 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> 

When the application starts, we get an 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 also tried to use the following, but we 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 
+5
source share
2 answers

After some work, I can deal with the problem. Below is the code that works 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 sleep mode 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 the request encoding format in the filter

 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 do not support special characters, check the database character set, try to recreate the database and use charset UTF8 instead of latin1

+7
source

When you install MySQL using the UI installer, you can set the default character set as "UTF-8". After that, everything works fine. I always installed this during the installation, so I don’t know how to install it later. But if you do not want to reinstall MySQL, check this link:

How to install utf-8 as a standard character set for mysql server?

0
source

Source: https://habr.com/ru/post/984392/


All Articles