由于SpringMVC的默认编码问题(不支持中文),所以在@ResponseBody里返回中文时会出现乱码。
方法一,在XML文档里写入全局设置,改为UTF8
<context:component-scan base-package="base.code.controller"/> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
id="internalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"/>
<property name="suffix" value=".html"/>
方法二,在@RequestMapping中添加produces = "text/html;charset=utf-8",但是次方法只适用于局部。
原文链接:https://www.f2er.com/springmvc/421910.html