161912. Tool class: SPRING+SPRINGMVC custom code Conversion

Source: Internet
Author: User

a custom class (note the properties in which the configuration in Web. XML is based on this class)ImportOrg.springframework.web.filter.OncePerRequestFilter;ImportJavax.servlet.FilterChain;Importjavax.servlet.ServletException;Importjavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse;Importjava.io.IOException;Importjava.io.UnsupportedEncodingException;ImportJava.util.Iterator;/*** Filter-encoded format conversion*/ Public classEncodingconvertfilterextendsOnceperrequestfilter {/**original encoding format: corresponding settings in the Web. xml File*/    PrivateString fromencoding = "Iso-8859-1"; /**destination encoding format: corresponding settings in the Web. xml File*/    PrivateString toencoding = "UTF-8"; @SuppressWarnings ("Unchecked") @Overrideprotected voidDofilterinternal (HttpServletRequest request, httpservletresponse response, Filterchain Filterchain)throwsservletexception, IOException {if(Request.getmethod (). Equalsignorecase ("GET")) {             for(Iterator<string[]> Iterator =Request.getparametermap (). values (). iterator (); Iterator.hasnext ();) {string[] parames=Iterator.next ();  for(inti = 0; i < parames.length; i++) {                    Try{Parames[i]=NewString (Parames[i].getbytes (fromencoding), toencoding); } Catch(unsupportedencodingexception e) {e.printstacktrace ();    }}}} filterchain.dofilter (request, response); }    /*** Get the original encoding format *@returnOriginal encoding format*/     PublicString getfromencoding () {returnfromencoding; }    /*** Set the original encoding format *@paramfromencoding * Original encoding format*/     Public voidsetfromencoding (String fromencoding) { This. fromencoding =fromencoding; }    /*** Get target encoding format *@returnDestination encoding format*/     PublicString gettoencoding () {returntoencoding; }    /*** Set Target encoding format *@paramtoencoding * Target encoding format*/     Public voidsettoencoding (String toencoding) { This. toencoding =toencoding; }} II, web. XML<!--Springmvc Self-setting filter post--><filter> <filter-name>encodingFilter</filter-name> <filter-class>org.springframework.web.filter.characterencodingfilter</filter-class> <!--where encoding is used to set the encoding format, forceencoding is used to set whether to ignore the request.getcharacterencoding () method, set to true to force overwrite the previous encoding format--&    Lt;init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter><!--custom Filter GET requests if uriencoding= "is configured in the Server.xml file in Tomcat UTF-8 ", can not need-<filter> <filter-name>encodingConvertFilter</filter-name> <filter-class>home.xxx.filter.encodingconvertfilter</filter-class> <init-param> <param-name>fromEncoding</param-name> <param-value>iso-8859-1</p aram-value> </init-param> <init-param> <param-name>toEncoding</param-name> < Param-value>utf-8</param-value> </init-param> </filter><filter-mapping>

161912. Tool class: SPRING+SPRINGMVC custom code Conversion

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.