前端get請求時會默認編碼,導(dǎo)致后臺取數(shù)據(jù)亂碼责静,解決方案除了在web.xml里面加filter 過濾外徽缚,還要在tomcat 配置文件server.xml里面設(shè)置 Connector的URIEconding=“UTF-8"贩汉。
我的項目是spring項目驱富,web.xml配置如下:
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filte-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>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
server.xml配置
<Connector port="8081" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" URIEncoding="UTF-8"/>