2011-12-14 2 views
0

Я не могу заставить спящий режим писать символы utf8 в базу данных. Если я вручную напишу их с помощью phpmyadmin или аналогичных инструментов, он будет отображаться в Интернете, и все будет в порядке. Но если я mannualy заполнить форму и отправить ее, то использовать спящий режим, чтобы сохранить его в DB он дает įųÄųÄ. Все в MySQL является UTF-8:Java EE + Hibernate + Glassfish 3.1 + MySQL 5.5.16 -> utf не работает

mysql> SHOW VARIABLES LIKE 'collation%'; 
+----------------------+-------------------+ 
| Variable_name  | Value    | 
+----------------------+-------------------+ 
| collation_connection | utf8_general_ci | 
| collation_database | utf8_general_ci | 
| collation_server  | utf8_general_ci | 
+----------------------+-------------------+ 

mysql> SHOW VARIABLES LIKE 'character_set%'; 
+--------------------------+----------------------------+ 
| Variable_name   | Value      | 
+--------------------------+----------------------------+ 
| character_set_client  | utf8      | 
| character_set_connection | utf8      | 
| character_set_database | utf8      | 
| character_set_filesystem | binary      | 
| character_set_results | utf8      | 
| character_set_server  | utf8      | 
| character_set_system  | utf8      | 
| character_sets_dir  | /usr/share/mysql/charsets/ | 
+--------------------------+----------------------------+ 

Все таблицы/столбцы VARCHAR таблиц utf-8 general.

Мой файл гибернации-config.xml:

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xmlns:p="http://www.springframework.org/schema/p" 
     xmlns:tx="http://www.springframework.org/schema/tx" 
     xmlns:context="http://www.springframework.org/schema/context" 
     xsi:schemaLocation=" 
    http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd 
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.0.xsd 
     "> 

    <!-- Enable annotation style of managing transactions --> 
    <tx:annotation-driven transaction-manager="transactionManager" /> 

    <!-- Declare the Hibernate SessionFactory for retrieving Hibernate sessions --> 
    <!-- See http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/orm/hibernate3/annotation/AnnotationSessionFactoryBean.html -->  
    <!-- See http://docs.jboss.org/hibernate/stable/core/api/index.html?org/hibernate/SessionFactory.html --> 
    <!-- See http://docs.jboss.org/hibernate/stable/core/api/index.html?org/hibernate/Session.html --> 
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <!-- init-method="createDatabaseSchema" --> 
     <property name="dataSource" ref="dataSource" /> 
     <property name="packagesToScan" value="lt.database.model" /> 
     <property name="hibernateProperties"> 
      <props> 
       <prop key="hibernate.connection.useUnicode">true</prop> 
       <prop key="hibernate.connection.characterEncoding">UTF-8</prop> 
       <prop key="hibernate.connection.charSet">UTF-8</prop>     
       <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> 
       <prop key="hibernate.show_sql">true</prop> 
      </props> 
     </property> 
    </bean> 

    <!-- Declare a datasource that has pooling capabilities--> 
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" 
    destroy-method="close" 
    p:driverClass="com.mysql.jdbc.Driver" 
    p:jdbcUrl="jdbc:mysql://localhost/aukcionas?autoReconnect=true&amp;useUnicode=true&amp;characterEncoding=UTF-8" 
    p:user="root" 
    p:password="" 
    p:acquireIncrement="5" 
    p:idleConnectionTestPeriod="60" 
    p:maxPoolSize="100" 
    p:maxStatements="50" 
    p:minPoolSize="10" /> 

    <!-- Declare a transaction manager--> 
    <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager" 
      p:sessionFactory-ref="sessionFactory" /> 

</beans> 

Так почему я до сих пор не могу получить эту вещь, чтобы работать? Я использую:

  • GlassFish 3.1
  • MySQL 5.5.16
  • Hibernate 3
  • Spring 3.0.2
  • Spring Security 3.0.7
+0

Вы уверены, что правильное значение передается в спящий режим - вы зарегистрировали значение непосредственно перед вызовом API гибернации для проверки? – gkamal

ответ

0

Fixed сам. Добавьте это в файл web.xml:

<filter> 
     <filter-name>encodingFilter</filter-name> 
     <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> 
     <init-param> 
      <param-name>encoding</param-name> 
      <param-value>UTF-8</param-value> 
     </init-param> 
     <init-param> 
      <param-name>forceEncoding</param-name> 
      <param-value>true</param-value> 
     </init-param> 
    </filter> 
    <filter-mapping> 
     <filter-name>encodingFilter</filter-name> 
     <url-pattern>/*</url-pattern> 
    </filter-mapping> 
Смежные вопросы