ssm中从页面到controller和数据库出现乱码问题的解决
1.确保项目编码为utf8,点击项目右键,点击properties
2.确保数据库编码为utf8,以MySQL为例,可到mysql目录下,my.ini文件中修改后,重启mysql服务
重启mysql服务方法:
点击计算机右键,管理,重新启动mysql
3.数据库建库时,确认编码为utf-8,以使用Sqlyog为例
4.jsp页面
顶端加入<%@ page language="Java" import="java.util.*" pageEncoding="UTF-8"%>
Head中加入
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
表单提交方式设为"Post"
5.如果使用spring,在web.xml中加入
<filter>
<filter-name>characterEncodingFilter</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>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
6.在controller/action中,设置
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
7.TOMCAT设置
conf-server.xml
<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000"
redirectPort="8443" URIEncoding="UTF-8" useBodyEncodingForURI="true"/>