1.在代码中添加


request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");

2.在扫描mvc时添加以下内容

<mvc:annotation-driven>
	<mvc:message-converters>
		<!-- 原生ajax 中文编码处理 -->
		<bean class="org.springframework.http.converter.StringHttpMessageConverter">
			<property name="supportedMediaTypes">
				<list>
					<value>text/html;charset=utf-8</value>
				</list>
			</property>
		</bean>
		<!-- fastjson 中文编码处理 -->
		<bean
			class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
			<property name="supportedMediaTypes">
				<list>
					<value>text/html;charset=utf-8</value>
					<value>application/json;charset=utf-8</value>
				</list>
			</property>
		</bean>
	</mvc:message-converters>
</mvc:annotation-driven>

3.在web.xml中添加以下内容

<!-- form 表单post 中文乱码 -->
<filter>
	<filter-name>formPostData</filter-name>
	<!-- CharacterEncodingFilter 过滤 -->
	<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>formPostData</filter-name>
	<!-- 拦截所有页面请求(图片等不拦截) -->
	<url-pattern>/*</url-pattern>
</filter-mapping>


标签: spring, 中文乱码

添加新评论