web項目和SpringMvc集成的時候出現中文亂碼html
post:jsp頁面java
<body> ${username} <form action="myController" method="post"> <input name="username" type="text" > <input type="submit" value="提交" > </form> </body>
java後臺:web
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception { String username = request.getParameter("username"); System.out.println(username); ModelAndView model = new ModelAndView(); model.addObject("username", username); System.out.println(username); return model; }
出現亂碼:æ¯å¦æçå
解決方式:在web.xml文件裏面添加filter,注意這種方式只能解決post方式的亂碼ajax
<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> </filter> <filter-mapping> <filter-name>characterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
post:亂碼問題解決spring
------------------------------------------------------------------------------------------------------------------瀏覽器
get:亂碼tomcat
ajax,a標籤,form,瀏覽器地址欄輸入的參數都有可能出現get方式的請求服務器
這種方式在和具體服務器的配置有關app
好比tomcat解決方式jsp
在server.xml裏面
將
<Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443" />
修改爲
<Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443" URIEncoding="UTF-8"/>