Strut2 + MySql + Hibernate + ajax(jQuery)中文亂碼解決辦法:
1. 網頁頁面使用utf-8
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
2. ajax默認使用utf-8傳輸數據, 但最好使用post, 不要使用get
3. Struts2中使用UTF-8.
3.1 在struts.properties中加上: struts.i18n.encoding=UTF-8
[3.2 使用Filter: 從頁面中傳輸數據到Struts2, 需要使用過濾器把數據在交給struts2前轉換成utf-8的: http://stevieliu.blogchina.com/stevieliu/6223098.html]
試過不使用3.2照常可以收到中文.
4. 數據庫也要使用utf-8, 在建表時設置好.
http://imysql.cn/charset_tips
5. hibernate.cf.xml配置文件中與數據庫的連接符串也要標明使用的編碼為utf-8: <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/mlm?useUnicode=true&characterEncoding=UTF-8</property>
6: Tomcat也使用UTF-8
6.1: Tomcat/bin/catalina.bat中開始部分(大概51行處)添加set CATALINA_OPTS=-Dfile.encoding=UTF-8
或CATALINA_OPTS="$CATALINA_OPTS -Dfile.encoding=UTF-8"
6.2: <Connector URIEncoding="utf-8" port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
Eclipse啟動Tomcat會修改Tomcat的運行編碼環境,從終端啟動的又是另一種編碼環境,所以Tomcat的配置也要手動修改
7: HttpServletResponse(request)也要設置編碼:response.
setCharacterEncoding("UTF-8");