乱码问题:
在javaweb开发中三大问题之一:乱码,经常遇到,今天在这儿总结一下
关于编码:
UTF-8(8-bit Unicode Transformation Format)是一种针对Unicode的可变长度字符编码,又称万国码。我们需要做的是从网页,编译环境,数据库字符集使用统一字符即可解决javaweb开发中的乱码问题
0.jsp文件使用utf-8编码
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
1.web.xml中spring提供的字符编码过滤器
<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>
2.连接数据库字符串
jdbc_url=jdbc:mysql://115.159.142.241:3306/computer?useUnicode=true&characterEncoding=UTF-8
3.spring配置json
<bean id="mappingJacksonHttpMessageConverter"
class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>application/json;charset=utf-8</value>
</list>
</property>
</bean>
4.tomcat容器 server.xml
<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" URIEncoding="UTF-8"/>
5.数据库字符编码和表字段的字符编码 设置utf8_general_ci