Use SPRINGMVC @responsebody return the type of the specified data as the HTTP body outward output, the content returned in the browser has Chinese, will appear garbled, the project encoding, tomcat encoding, etc. have been set to Utf-8, the following is a string of Chinese garbled.
Java code 650) this.width=650; "class=" star "src=" Http://assen.iteye.com/images/icon_star.png "alt=" Favorite Code "style=" border : 0px; "/>
@RequestMapping ("User/get_comment_list.do")
Public @ResponseBody String getusercommentlist (Integer user_id,byte type) {
hashmap<string, object> map = new hashmap<string, object> ();
Map.put ("type", type);
Map.put ("user_id", user_id);
Commentactpojo Actpojo = new Commentactpojo ();
list<commentinfo> list = this. bo.getcomlist (map);
Actpojo.setcomments (list);
//system.out.println ("Data:" +jsonutil.tojson (Actpojo));//Print Data no Chinese garbled
return Jsonutil.tojson (Actpojo);
}
Springmvc use of the version is 3.2.2, later on the internet to find some information, in the @requestmapping mandatory to specify the type of data returned and character encoding, Chinese garbled solution, as follows:
Java code 650) this.width=650; "class=" star "src=" Http://assen.iteye.com/images/icon_star.png "alt=" Favorite Code "style=" border : 0px; "/>
-
@RequestMapping (Value= "user/get_comment_list.do" , Produces = " Application/json; charset=utf-8 ")
Java code 650) this.width=650; "class=" star "src=" Http://assen.iteye.com/images/icon_star.png "alt=" Favorite Code "style=" border : 0px; "/>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="Http://www.springframework.org/schema/beans"
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-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/MVC
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd ">
<!--must be in <mvc:annotation-driven> before--
<bean Class="Org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter ">
<property name="Messageconverters">
<list>
<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>
<value>applicaiton/javascript;charset=utf-8</value>
</list>
</property>
</bean>
</list>
</property>
</bean>
<!--scan project Files--
<context:component-scan base-package ="Com.tcl.club.core" />
<context:component-scan base-package ="Com.cus.back" >
<context:exclude-filter type="annotation" expression="Org.springframework.stereotype.Service" />
</context:component-scan>
<mvc:annotation-driven/>
<bean id="Multipartresolver"
class="Org.springframework.web.multipart.commons.CommonsMultipartResolver" />
<!--the resolution of the model view name by adding a prefix to the Model view name, which is automatically called after the address entered by the Requestmapping for view resolution-
<bean id="Viewresolver"
class="Org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="Viewclass"
value="Org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/web-inf/view/" />
<property name="suffix" value=". jsp" />
</bean>
</beans>
The Springmvc.xml file above re-sets the encoding of the stringhttpmessageconverter without having to set the produces attribute in each @requestmapping. If you return the type of Jackson data, you can set it as follows:
Java code 650) this.width=650; "class=" star "src=" Http://assen.iteye.com/images/icon_star.png "alt=" Favorite Code "style=" border : 0px; "/>
<bean
class="Org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="Supportedmediatypes">
<list>
<value>application/json; charset=utf-8</value>
<value>application/x-www-form-urlencoded; charset=utf-8</value>
</list>
</property>
</bean>
SPRINGMVC return @responsebody Chinese garbled