recently in the project use process found in the SPRINGMVC project, using the return page request method, the data can be displayed normally, but for Ajax requests, garbled characters are always displayed.
First of all, because we have a spring character encoding filter configured in Web. XML, why not use AJAX requests?
The following simple analysis, for reference only.
The simple request code is listed first:
// java code @Controller @requestmapping ( "Goods" ) public class Goodscontroller {@RequestMapping (Value = "page" ) @ResponseBody public String page () {jsonobject Jo = ne W Jsonobject (); Jo.put ( "page", "Test" ); return jo.tostring (); } }
<!--Ajax code---$.ajax ({"/easy_buy_ssm/goods/page.action", "POST", "Application/x-www-form-urlencoded", "JSON", function(data) { var page = data.page; var goodslist = page.newslist; Apendgoods (goodslist); }});
<!--Web. XML - <Filter> <Filter-name>Encodingfilter</Filter-name> <Filter-class>Com.smy.util.EncodingFilter</Filter-class> <Init-param> <Param-name>Encode</Param-name> <Param-value>UTF-8</Param-value> </Init-param> </Filter> <filter-mapping> <Filter-name>Encodingfilter</Filter-name> <Url-pattern>/*</Url-pattern> </filter-mapping>
first, when a request arrives, it passes through spring's filter characterencodingfilter, when the filter is set, the encoding will
into the springmvc of this dispatcherservlet, through Springmvc a series of conversions (omitted here ...) ), reach our control layer,
and help us encapsulate the parameters. After you configure this configuration item in Springmvc <mvc:annotation-driven>
, it is configured by default
Requestmappinghandleradapter and Httpmessageconverter, when we use @responsebody, the data is returned
This data converter is called. After viewing the source, it is converted to ISO-8859-1 format by default.
Simple source attached:
Public classStringhttpmessageconverterextendsAbstracthttpmessageconverter<string> { Public Static FinalCharset default_charset = Charset.forname ("Iso-8859-1"); Private FinalCharset Defaultcharset; Private FinalList<charset>availablecharsets; Private BooleanWriteacceptcharset; protected voidwriteinternal (String s, httpoutputmessage outputmessage)throwsIOException {if( This. Writeacceptcharset) {outputmessage.getheaders (). Setacceptcharset (Getacceptedcharsets ()); } Charset Charset=Getcontenttypecharset (Outputmessage.getheaders (). getContentType ()); Streamutils.copy (S, CharSet, Outputmessage.getbody ()); } ...} Public Abstract classAbstracthttpmessageconverter<t>ImplementsHttpmessageconverter<T> { protected FinalLog logger = Logfactory.getlog (Super. GetClass ()); PrivateList<mediatype> supportedmediatypes =collections.emptylist (); ...}
From the above source can be seen, for the string will be automatically encoded into the default format iso-8859-1, so the corresponding solution.
Programme one:
@RequestMapping (value = "/test", produces= "TEXT/HTML;CHARSET=UTF-8;")
Scenario Two:
Note that it is important to use the spring 3.1.x above.
<Mvc:annotation-driven> <mvc:message-convertersRegister-defaults= "true"> <Beanclass= "Org.springframework.http.converter.StringHttpMessageConverter"> < Propertyname= "Supportedmediatypes"value= "Text/html;charset=utf-8"/> </Bean> </mvc:message-converters> </Mvc:annotation-driven>
Programme III:
Instead of using @responsebody, change the request processing to the following:
// Java Code @Controller @requestmapping ("Goods") Public class Goodscontroller { @RequestMapping (value= "page") public void page ( HttpServletResponse response) { new jsonobject (); Jo.put ("page", "Test"); Response.getwriter (). Write (jo.tostring ()); } }
SPRINGMVC Ajax Request page display garbled