2015年7月26日 星期日

Eclipse 的 Spring MVC project 傳送表單修改資料庫時遇到編碼問題的解決辦法

因為這問題可能的原因很多,所以要一個一個確認
  1. 先確認eclipse的編碼
  2. 確認project中檔案的編碼
  3. 確認 hibernate 的編碼
    如果是因為這個原因,直接直接在jdbc的dabase url 上註明其編碼
    <property name="hibernate.connection.url" value="utf8" >jdbc:mysql://127.0.0.1/MyTable?characterEncoding=utf8</property>
  4. 修改Project Folder/src/main/webapp/WEB-INF/web.xml,於web-app tag中加入下方code
    <filter>
    	<filter-name>setCharacterEncoding</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>setCharacterEncoding</filter-name> 
    	<url-pattern>/*</url-pattern> 
    </filter-mapping>

參考資料

Hibernate character encoding, postgresql. (2010, 9 7). Retrieved from https://forum.hibernate.org/viewtopic.php?f=1&t=1006879

沒有留言:

張貼留言