Spring MVC 用post方式提交表单到Controller乱码问题,而get方式提交没有乱码问题

时间:2021-12-31 13:30:19

在web.xml中添加一个filter,即可解决post提交到Spring MVC乱码问题

<!-- 配置请求过滤器,编码格式设为UTF-8,避免中文乱码-->
<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>