doGet方法返回的中文值乱码的问题(spring mvc)

现在遇到的有两种情况

第一种:

  response中返回的string字符串乱码

解决方法:

 OutputStream os = null;

os = response.getOutputStream();

 response.setHeader("Content-type", "text/html;charset=UTF-8");  
 String data = "中国";  
//这句话的意思,使得放入流的数据是utf8格式  

os.write(data.getBytes("UTF-8")); 

第二种:

    return返回的中文字符串乱码

则在spring配置文件里面(默认是application.xml)追加下面代码

<!-- 解决返回String时乱码 return 字符串,前台默认让它成为html形式的UTF-8 -->

<mvc:message-converters register-defaults="true">
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/plain;charset=UTF-8</value>
<value>text/html;charset=UTF-8</value>
</list>
</property>
</bean>
</mvc:message-converters>

</mvc:annotation-driven>

修改前:


修改后:




猜你喜欢

转载自blog.csdn.net/danshangu/article/details/80351047