我在使用Hibernate從表單保存數據時遇到問題,基本上編碼出錯,所以奇怪的字符被保存在數據庫中,並作爲表單輸入中的結果填充。這是我的配置迄今製備:在保存實體時出現Spring/Hibernate編碼錯誤
在web.xml
<filter>
<filter-name>SetCharacterEncodingFilter</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>SetCharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
在applicationContext.xml中
(嘗試都圖案 'UTF-8' 和 'UTF8')
<property name="url" value="jdbc:postgresql://localhost:5432/mydb?characterEncoding=UTF-8" />
.. 。
<property name="hibernateProperties">
<props>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQL9Dialect</prop>
<prop key="hibernate.connection.CharSet">utf8</prop>
<prop key="hibernate.connection.characterEncoding">utf8</prop>
<prop key="hibernate.cache.use_query_cache">true</prop>
<prop key="hibernate.cache.use_second_level_cache">true</prop>
<prop key="hibernate.cache.region.factory_class">
org.hibernate.cache.ehcache.EhCacheRegionFactory
</prop>
<prop key="hibernate.connection.useUnicode">true</prop>
<prop key="hibernate.temp.use_jdbc_metadata_defaults">false</prop>
<prop key="show_sql">true</prop>
<prop key="hibernate.c3p0.min_size">5</prop>
<prop key="hibernate.c3p0.max_size">20</prop>
<prop key="hibernate.c3p0.timeout">300</prop>
<prop key="hibernate.c3p0.max_statements">50</prop>
<prop key="hibernate.c3p0.idle_test_period">3000</prop>
</props>
</property>
的pom.xml
<properties>
<spring.version>4.1.2.RELEASE</spring.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
數據庫,jsp文件和項目也有ut8編碼,我還在server.xml中的連接器配置中添加了URIEncoding =「UTF-8」。我還需要補充什麼,或者我做錯了什麼?
。你檢查過列的編碼了嗎? –
我正在使用postgres 9,我能夠插入正確的值與原始查詢,並在以後查看正確顯示它們,問題發生後,我嘗試從應用程序窗體保存的東西,我使用@Valid註釋和BindingResult對象作爲行動的參數負責保存數據 – Markownikow