In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the knowledge of "how to configure CharacterEncodingFilter in Spring". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
The configuration file for spring is as follows:
Org.springframework.web.context.ContextLoaderListener contextConfigLocation classpath:applicationContext.xml mybatis org.springframework.web.servlet.DispatcherServlet contextConfigLocation classpath:mybatis-servlet.xml 1 mybatis / SpringEncodingFilter org.springframework.web.filter.CharacterEncodingFilter Encoding UTF-8 forceEncoding true SpringEncodingFilter / *
There are many headaches in the project, among which the coding problem is one of them, so how to solve the coding problem of the string coming from the page in the Spring framework? Let's take a look at the Spring framework that provides us with the filter CharacterEncodingFilter.
1. See the structure clearly:
You can see that it inherits GenericFilterBean and OncePerRequestFilter, that is, this filter is designed to filter every browser request, and then adds a function that the parent class does not have, that is, dealing with character encoding.
two。 Official explanation:
Servlet 2.3/2.4 Filter that allows one to specify a character encoding for requests. This is useful because current browsers typically do not set a character encoding even if specified in the HTML page or form. (this means that it is useless to set the code in a html page or form.)
This filter can either apply its encoding if the request does not already specify an encoding, or enforce this filter's encoding in any case ("forceEncoding" = "true"). (as long as you set foreEncoding=true, it is useless to set the encoding format in the code,) In the latter case, the encoding will also be applied as default response encoding on Servlet 2.4 + containers (although this will usually be overridden by a full content type set in the view).
3. How to use
Let's take a look at how to configure in web.xml:
EncodingFilter
Org.springframework.web.filter.CharacterEncodingFilter
Encoding
UTF-8
ForceEncoding
True
Encoding is used to set the encoding format, forceEncoding is used to set whether to ignore the request.getCharacterEncoding () method, and setting to true forces the previous encoding format to be overwritten.
4. Source code appreciation
When the Servlet container starts, it reads the configuration information for the filter in web.xml, reads the values corresponding to the child tags encoding and forceEncoding in, and then injects the values into these fields by calling the classes setEncoding (String encoding) and setForceEncoding (boolean forceEncoding).
Here you can see why setting foreEncoding to true overrides the method in request.getCharacterEncoding (). Hehe, there is no secret before the source code, and only by going deep into the source code can you see the essence.
EncodingFilter
Org.springframework.web.filter.CharacterEncodingFilter
Encoding
UTF-8
EncodingFilter
/ *
This is the end of "how to configure CharacterEncodingFilter in Spring". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.