今天測試spring mvc ,中文亂碼,在web.xml中加上java
<filter> <filter-name>encodingFilter</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>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
過濾全部請求,但仍是有問題,最後發現是表單的問題,在表單中method默認是get,而以上過濾器只對post有效果,若是是 get,須要修改 Tomcat根式目錄的 conf/server.xml文件中,找<Connector port="8080" />,在裏面加<Connector port="8080" uRIEncoding="utf-8" />web
關於Get的中文亂碼,除了根據上面所述更改tomcat server.xml的方式生效外,還能夠對參數進行轉碼,以下:spring
name = new String(name.getBytes("iso-8859-1"),"utf-8");tomcat
這樣也能夠獲得一樣正確的結果
mvc