ajax请求参数为乱码

1.前端JSP:

<%@ page contentType="text/html;charset=UTF-8" pageEncoding="utf-8" %>

2.web.xml

<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>

3.springmvc.xml

<mvc:annotation-driven>
     <mvc:message-converters register-defaults="true">
      <!--StringHttpMessageConverter默认编码为ISO8859-1 -->
      <bean class="org.springframework.http.converter.StringHttpMessageConverter">
          <property name="supportedMediaTypes">
           <list>
            <value>text/plain;charset=UTF-8</value>
           <!-- <value>application/json;charset=UTF-8</value> -->
           </list>
          </property>
      </bean>
    </mvc:message-converters>
     </mvc:annotation-driven>

4.TOMCAT:

server.xml中 <Connector port="8080" 加入URIEncoding="UTF-8"

通过这四步应该可以解决此问题。

相关推荐