struts 2 乱码问题
请教下,struts2 的乱码问题,
1、jsp页面设置的编码是utf-8
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
2、struts.xml
<constant name="struts.i18n.encoding" value="utf-8"></constant>
<constant name="struts.locale" value="zh_CN"></constant>
3、在web.xml中加了filter
public void doFilter(ServletRequest req, ServletResponse resp,
FilterChain arg2) throws IOException, ServletException {
// TODO Auto-generated method stub
HttpServletResponse response = (HttpServletResponse) resp;
HttpServletRequest request=(HttpServletRequest)req;
response.setContentType("text/html;charset=utf-8");
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
arg2.doFilter(request, response);
}
4、mysql数据库中
[mysql]
default-character-set=utf8
character-set-server=utf8
5、tomcat 的 service.xml中
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" URIEncoding="UTF-8"/>
6、在console接收参数时后台打印的也正常,包括构造sql语句的输出也是正常的,但是在struts的调试模式还是存在乱码,,崩溃中!!!!!!
--------------------编程问答--------------------
可有人在呀,,求帮忙啊。 --------------------编程问答-------------------- 你连接数mysql据库的jdbc url怎么写的?
是否是使用perparstatement insert的吗?
有加这句吗?
jdbc://localhost:3306/数据库?useUnicode=true&characterEncoding=UTF-8 --------------------编程问答-------------------- url是这么写的。 没有错。 --------------------编程问答-------------------- debug下,看插入之前的数据是否是乱码不久行了吗 --------------------编程问答--------------------
debug了, 在数据插入前都是正常的,在传到struts 中的时候,senderror时候出错啦。 --------------------编程问答-------------------- 数据库编码是什么
是utf8吗 --------------------编程问答-------------------- 乱码是个技术活,以下是以前解决的办法:
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");
补充:Java , Java EE