Java: Strut2 + MySql + Hibernate + ajax(jQuery)中文乱码解决办法

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6


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&amp;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");

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6
标签: mysqlJava

“Java: Strut2 + MySql + Hibernate + ajax(jQuery)中文乱码解决办法” 的相关文章