Spring3.0 MVC @ResponseBody garbled problem solving __ garbled problem

Source: Internet
Author: User
Tags xmlns

Recently in the project refactoring, the original project Struts2 replaced the spring3.0 mvc, personal feeling Spring3 mvc than struts2 development more convenient. Among other things its restful URLs. More information on Spring3 MVC Online is also a lot of, here is not introduced.

The purpose of writing this article is to record the problem of solving Spring3.0 MVC @ResponseBody Chinese garbled characters.

The role of the Spring3.0 MVC @ResponseBody is to write the return value directly into the HTTP response body. Implement the Annotationmethodhandleradapter class Handleresponsebody method concretely, implement the code concretely:

  private void Handleresponsebody (Object returnvalue, Servletwebrequest webRequest) throws Servletexception, IOException {httpinputmessage inputmessage = new Servletserverhttprequest (Webrequest.getrequest ()); list<mediatype> acceptedmediatypes = Inputmessage.getheaders (). Getaccept (); if (Acceptedmediatypes.isempty ()) {acceptedmediatypes = Collections.singletonlist (Mediatype.all);} Httpoutputmessage outputmessage = new Servletserverhttpresponse (Webrequest.getresponse ()); class<?> Returnvaluetype = Returnvalue.getclass (); list<mediatype> allsupportedmediatypes = new arraylist<mediatype> (); if (messageconverters! = null) {for (Httpmessageconverter messageconverter:messageconverters) {allsupportedmediatypes . AddAll (Messageconverter.getsupportedmediatypes ()); for (mediatype acceptedmediatype:acceptedmediatypes) {if (Messageconverter.canwrite (Returnvaluetype, Acceptedmediatype) {messageconverter.write (returnvalue, NULL, outputmessage); This.responseargUmentused = true; Return }}}} throw new Httpmediatypenotacceptableexception (allsupportedmediatypes); } }

OK, understand the implementation of @responsebody, we return to garbled problem.

Based on the minimum configuration of SPRING3 MVC:

<beans xmlns= "Http://www.springframework.org/schema/beans" xmlns:xsi= "http://www.w3.org/2001/ Xmlschema-instance "xmlns:p=" http://www.springframework.org/schema/p "xmlns:context="/HTTP/ Www.springframework.org/schema/context "xsi:schemalocation=" Http://www.springframework.org/schema/beans/http Www.springframework.org/schema/beans/spring-beans-2.5.xsd Http://www.springframework.org/schema/context/HTTP Www.springframework.org/schema/context/spring-context-2.5.xsd "> <!--convert classes marked with @controller annotations to beans---< Context:component-scan base-package= "Com.acity.**.web"/> <!--start the Spring MVC Annotation feature, complete the request and annotation pojo mappings--<bean class= "Org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" > </bean> <!-- Resolution of the Model view name, that is, the model view name is added before and after the <bean id= "Defaultviewresolver" class= " Org.springframework.web.servlet.view.InternalResourceViewResolver "p:prefix="/admin/"p:suffix=". jsp "/> </ Beans>

The client initiates the request using the jquery Ajax method:

$.ajax ({type: "POST", url: "/admin.jspx, Datatype:string, Data:new goodsparameter (), success:function (data) {$ (" # Flitercontent "). htm (data); } } });

Result output garbled:???????

Debug trace @responsebody Implementation class found its default encoding is iso-8859-1, see picture yellow section

Understand its garbled principle we have to solve it.

Change the SPRINGMVC configuration file to:

<?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:p= "http://www.springframework.org/schema/p" xmlns:context= "Http://www.springframework.org/schema/context" xsi:schemalocation= "Http://www.springframework.org/schema/beans Http://www.springframework.org/schema/beans/spring-beans-2.5.xsd Http://www.springframework.org/schema/context Http://www.springframework.org/schema/context/spring-context-2.5.xsd "> <!--Convert a class labeled @controller annotation to a bean-- > <context:component-scan base-package= "com.acity.**.web"/> <!--start the Spring MVC Annotation feature, complete the request and annotation pojo mappings-- <bean class= "Org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" > <property Name= "Messageconverters" > <list> <bean class = " Org.springframework.http.converter.StringHttpMessageConverter "> <property name =" Supportedmediatypes "> <list> <value>text/plain;charset=utf-8</value> </list> </property> </bean> </list> </property> </bean > <!--resolution of the name of the model view, that is, the model view name is added before and after <bean id= "Defaultviewresolver" class= " Org.springframework.web.servlet.view.InternalResourceViewResolver "p:prefix="/admin/"p:suffix=". jsp "/> </ Beans>

This specifies the encoding by configuring the Annotationmethodhandleradapter Class Messageconverters property.

Seems to solve the problem, but it is not. Run discovery, Ajax has no data to return. Get Google Chrome debugging found:

Failed to load Resource:the Server responded with a status of 406 (not acceptable)

Continue debug to find this exception thrown by the Handleresponsebody method. Because the mediatype type requested by the client is defaulted by default: */*

So as long as we set the MIME type on the client, the problem can be solved.

The workaround uses the Beforesend method provided by jquery Ajax to set the type of its request acceptance, as follows:

Beforesend:function (XMLHttpRequest) {Xmlhttprequest.setrequestheader ("Accept", "Text/plain");}

So the problem is solved.

This is the younger brother's solution, do not know the heroes have no good solution, welcome to enlighten.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.