When the Chinese garbled problem occurs, spring can be resolved with the Characterencodingfilter filter, as shown in the following code:
<!--Spring character encoding filter: Solve Chinese garbled problem - <Filter> <Filter-name>Characterencodingfilter</Filter-name> <Filter-class>Org.springframework.web.filter.CharacterEncodingFilter</Filter-class> <Init-param><!--Specify the encoding - <Param-name>Encoding</Param-name> <Param-value>UTF-8</Param-value> </Init-param> </Filter> <filter-mapping> <Filter-name>Characterencodingfilter</Filter-name> <Url-pattern>/*</Url-pattern> </filter-mapping>
However, the filter only does not resolve a specific request, that is, the request to send Chinese in the address bar in a get way, for example:
Localhost:8080/myapp/user/toadd.action?name= xiaoming
Characterencodingfilter source code is as follows:
/** Copyright 2002-2007 the original author or authors. * Licensed under the Apache License, Version 2.0 (the "Licens E "); * You are not a use this file except in compliance with the License. * Obtain a copy of the License at * *http://www.apache.org/licenses/LICENSE-2.0* * Unless required by applicable or agreed to writing, software * Distributed under the License is distribute D on ' As is ' BASIS, * without warranties or CONDITIONS of any KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ PackageOrg.springframework.web.filter;Importjava.io.IOException;ImportJavax.servlet.FilterChain;Importjavax.servlet.ServletException;Importjavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse;/*** Servlet 2.3/2.4 Filter, allows one to specify a character encoding for * requests. This is useful because current browsers typically does not set a * character encoding even if specified in the HTML page or Form. * * <p>this filter can either apply its encoding if the request does not * already specify an encoding, or enforce t His filter's encoding in any case * ("forceencoding" = "true"). In the latter case, the encoding would also be * applied as Default response encoding on Servlet 2.4+ containers (although * This'll usually is overridden by a full content type set in the view). * * @authorJuergen Hoeller *@since15.03.2004 *@see#setEncoding *@see#setForceEncoding *@seejavax.servlet.http.httpservletrequest#setcharacterencoding *@seejavax.servlet.http.httpservletresponse#setcharacterencoding*/ Public classCharacterencodingfilterextendsOnceperrequestfilter {PrivateString encoding; Private BooleanForceencoding =false; /*** Set the encoding to use for requests. This encoding'll is passed into a * {@linkjavax.servlet.http.httpservletrequest#setcharacterencoding} call. * <p>whether This encoding would override existing request encodings * (and Whether It'll be applied as default Response encoding as well) * depends on the {@link#setForceEncoding "forceencoding"} flag. */ Public voidsetencoding (String encoding) { This. Encoding =encoding; } /*** Set whether the configured {@link#setEncoding Encoding} of this filter * are supposed to override existing request and response encodings. * <p>default is "false", i.e. does not modify the encoding if * {@linkjavax.servlet.http.httpservletrequest#getcharacterencoding ()} * Returns a Non-null value. Switch this to ' true ' to enforce the specified * encoding in any case, applying it as Default response encoding as Wel L. * <p>note that the response encoding would only is set on Servlet 2.4+ * containers, since Servlet 2.3 do not PR Ovide a facility for setting * A default response encoding. */ Public voidSetforceencoding (Booleanforceencoding) { This. forceencoding =forceencoding; } @Overrideprotected voiddofilterinternal (httpservletrequest request, httpservletresponse response, Filterchain Filterchain) throwsservletexception, IOException {if( This. Encoding! =NULL&& ( This. forceencoding | | request.getcharacterencoding () = =NULL) {request.setcharacterencoding ( This. Encoding); if( This. forceencoding) {response.setcharacterencoding ( This. Encoding); }} filterchain.dofilter (request, response); }}
about configuring the spring character encoding filter in Web. XML to solve the problem of Chinese garbled characters