SpringMvc防止乱码
时间: 2024-01-09 16:05:00 浏览: 89
在SpringMvc中防止乱码可以通过以下两种方式实现:
1. 在web.xml中配置过滤器,设置编码为UTF-8:
```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>
```
2. 在SpringMvc配置文件中添加以下配置:
```xml
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="UTF-8"/>
</bean>
```
阅读全文