A solution to the problem of Chinese garbled code in spring MVC
Garbled code is a headache. This paper introduces a solution to the problem of garbled code in spring MVC, as follows:
1: Solution to garbled code after the form submission controller obtains Chinese parameters
Note: the JSP page code is set to UTF-8
The form submission method must be post, and the spring coding filter under the get method does not work
Modify web XML, add the encoding filter as follows (note that the value of forceencoding parameter needs to be set to true)
Note: does the database code support Chinese
Are the database tables and table fields correct
Modify the parameter settings of the configuration connection database:
The first case:
JSP page Chinese input, to the controller garbled, at this time need to be set in the web An encoded filter is added to the XML file to unify the encoding into UTF-8. The code is as follows:
Web. XML configuration file:
It should be noted here that it is best to put this code at the beginning of webxml, because the interception is sequential. If it is placed later, it is easy to fail to intercept.
The second case:
For Chinese data in the database, the JSP page displays garbled code (not strictly garbled code, but in the form of question marks)
Because our front and back-end data interaction uses JSON data, I don't know the reason for this. I haven't encountered it before. I can only blame myself for doing too few projects, which is not difficult to solve. Just set the coding format when converting to JSON. The code is as follows:
Write this sentence and there will be no more random code.
The third case:
It is also correct to pass the Chinese page to the controller, but after saving it to the database, it is garbled (it is not strictly garbled, just like the above, it is full of question marks)
This problem bothered me for some time. I began to think that the coding format of the database was incorrect. It was still not possible to re create the database with the coding format of UTF-8. Finally, I thought it was the problem of JBoss. Our server used JBoss. We checked the data on the Internet and added the coding format when connecting to the data source. The code is as follows:
1. Page garbled
Page is relatively easy to solve. It is often enough to set the relevant character set on the corresponding JSP page or HTML page. as
2. Value transmission garbled code
In the process of value transmission, random code also occurs frequently. Let's not talk about the scenario first. The commonly used schemes are as follows: configure the specified filter
Set request character set
It is often garbled after passing in the corresponding controller or action from the foreground. My general idea is to print the default character set of the request itself first
Then, if the desired character set is not printed, set the corresponding character set as appropriate
Of course, a certain situation may still not be solved. At this time, use the following
3. Garbled code stored in the database
This is relatively complicated. MySQL is LZ used here, and MySQL is used to introduce how to solve the problem of garbled code
As we all know, whether the underlying layer uses pure JDBC, hibernate or JPA, it is actually JDBC in essence, and the corresponding framework only carries out specific encapsulation on the basis of correlation. Therefore, no matter what kind of technology, the URL connecting to the database will be used. So the URL needs to be checked first
url
In the standard case, the corresponding character set settings will be added after the corresponding, as shown below
As above, useunicode needless to say, connect to the character set set set in the database, & amp what is this? There is a problem, in XML & amp is the escape character of & amp. If you use XML to configure the corresponding database connection configuration, what's the problem. However, if * * properties is used, the amp must be removed. This is really LZ an experience of breaking one's head and bleeding one's blood.
database
The problem here is also relatively difficult to deal with. Log in to the database
View database encoding format
" = ">
You can see that the character set of the server is Latin1. Here we need to talk about the common character sets.
For the sake of world peace and prosperity, ISO has specified a set of Unicode character set scheme. Unicode coding is a bridge for mutual exchange and conversion between different codes, including 32-bit binary, so it can accommodate characters to the power of 2 to the 31st, which is enough in one's lifetime. According to different needs, Unicode is divided into three schemes.
Utf8: code used to solve problems arising from different languages in the world. 8 digits for English and 3 digits for Chinese. It can be displayed on any browser that supports utf9 character set without further processing.
The other two are utf16 and 32, which are no longer compiled here. You can check it by yourself. Generally, you decide which one to use because of the convenience of storage and use.
Well, another relatively familiar one is GBK, commonly known as national standard code, which is formulated according to Chinese national standards and only contains Chinese characters. Therefore, compared with the two, utf8 is more compatible, but has a larger storage capacity.
I'll be right back. I'll probably charge the character set, or I'll solve the problem first. So GBK or utf8 can be used here. However, Latin1 is definitely not allowed. This is mainly set through such a command
The codes of server, database and data table are set respectively. The connection code must be set. The connection code is set as follows:
After setting the code, you can successfully insert Chinese. In fact, it can be solved in one sentence
Common commands
View database encoding format
View the creation of tables in the database
Set database encoding format
The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.