在web.xml中添加一些代码
<filter>
<filter-name>encoding</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>
</filter>
<filter-mapping>
<filter-name>encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
仍有乱码需修改Tomcat配置信息并重启
打开安装Tomcat的文件,并找到conf路径下的server.xml文件,寻找代码段:
<Connector port="80" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" />
将以上代码修改为:
<Connector URIEncoding="UTF-8" port="80" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443"/>
然后保存配置文件即可,重新启动Tomcat就不会出现乱码了