Spring garbled problem Solution

Source: Internet
Author: User

Request garbled GET request garbled: Reason: request parameter with URL address, URL address when to parse? Tomcat received a request to encode the URL (iso8859-1) solution: Configure the 8080 port on Tomcat plus uriencoding= "Utf-8";
<uriencoding= "Utf-8"  connectiontimeout= "20000"  Port  = "8080"  protocol= "http/1.1"  redirectport= "8443"/> 
Post request garbled: Reason: The request parameter is the decoder with the request body is the first time to obtain the parameter filter can be used to solve garbled request.setcharacterencoding ("utf-8") response garbled Response.setcontenttype ("Text/html;charset=utf-8") 1, Tomcat one installation, get started to server.xml in the 8080 add uriencoding= "Utf-8" 2, Put a filter on the project to get him to solve all the post garbled; 3, the SPRINGMVC comes with a garbled solution of the filter project in Web. config:
<!--The filter for character encoding must be placed at the front -    <Filter>        <Filter-name>Characterencodingfilter</Filter-name>        <Filter-class>Org.springframework.web.filter.CharacterEncodingFilter</Filter-class>        <!--Configure encoding to tell us the encoding format specified -        <Init-param>            <Param-name>Encoding</Param-name>            <Param-value>Utf-8</Param-value>        </Init-param>        <!--Resolve response garbled -        <Init-param>            <Param-name>Forceencoding</Param-name>            <Param-value>True</Param-value>        </Init-param>    </Filter>    <filter-mapping>        <Filter-name>Characterencodingfilter</Filter-name>        <Url-pattern>/*</Url-pattern>    </filter-mapping>        <!--the filter that supports rest -    <Filter>        <Filter-name>Hiddenhttpmethodfilter</Filter-name>        <Filter-class>Org.springframework.web.filter.HiddenHttpMethodFilter</Filter-class>    </Filter>    <filter-mapping>        <Filter-name>Hiddenhttpmethodfilter</Filter-name>        <Url-pattern>/*</Url-pattern>    </filter-mapping>
Configuration inside the configuration of encoding and forceencoding look at Characterencodingfilter source, through Request.setcharacterencoding (this.encoding) and Response.setcharacterencoding (this.encoding) to troubleshoot post requests and response garbled questions:
 Public classCharacterencodingfilterextendsOnceperrequestfilter {PrivateString encoding; Private BooleanForceencoding =false;  Public voidsetencoding (String encoding) { This. Encoding =encoding; }     Public voidSetforceencoding (Booleanforceencoding) {         This. forceencoding =forceencoding; } @Overrideprotected voiddofilterinternal (httpservletrequest request, httpservletresponse response, Filterchain Filterchain) throwsservletexception, IOException {//encoding is UTF-8 .//forceencoding bit True        if( This. Encoding! =NULL&& ( This. forceencoding | | request.getcharacterencoding () = =NULL) {request.setcharacterencoding ( This. Encoding); if( This. forceencoding) {response.setcharacterencoding ( This. Encoding);    }} filterchain.dofilter (request, response); }}

Spring garbled problem solution

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.