Garbage code is a headache. This article introduces a solution to the problem of garbled Chinese in Spring MVC, which is as follows:
1: The solution for garbled code after the form submit controller obtains Chinese parameters
Note: jsp page encoding is set to UTF-8
The form submission method must be post. The spring encoding filter under the get method does not have any effect
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><form action="${ctx}/user/addUser" name="userForm" method="post">Modify web.xml and add encoding filters as follows (note that you need to set the forceEncoding parameter value to true)
<filter> <filter-name>characterEncodingFilter</filter-name> <filter-class>orgspringframeworkwebfilterCharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <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>
Note: Does the database encoding support Chinese
Are the database tables and table fields correct?
Modify the parameter settings of the configuration connection database:
<property name="url" value="jdbc:mysql://localhost:3306/dbname?useUnicode=true&characterEncoding=UTF-8"></property>
The first case:
Enter the jsp page in Chinese and enter the controller garbled code. At this time, what you need to set is to add an encoded filter (filter) to unify the encoding into UTF-8. The code is as follows:
Web.xml configuration file:
<filter><filter-name>CharacterEncodingFilter</filter-name><filter-class>orgspringframeworkwebfilterCharacterEncodingFilter</filter-class><init-param><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>
It should be noted here that it is best to place this code at the beginning of webxml, because there is an order of interception, and if placed at the back, it is easy to block it.
The second case:
Database Chinese data, jsp page displays garbled code (not garbled code in the strict sense, but is presented in the form of a question mark)
Since the data interaction between our front and backend uses json data, I don’t know much about the reason for this situation. I have never encountered it before. I can only blame myself for having done too few projects, and it is not difficult to solve it. I just need to set the encoding format when converting json. The code is as follows:
responsesetContentType("application/json;charset=UTF-8");// Prevent data transmission from garbled codeIf you write this sentence, you won’t have any garbled codes again.
The third situation:
The page is also correct to pass it to the controller, but after saving it to the database, it will be garbled (it is not a strict garbled code, just like the above, there are all question marks)
This problem has troubled me for a while. I started to feel that the encoding format of the database is incorrect. It is still not possible to recreate the database with the encoding format of utf-8. Finally, I felt that it was a problem with jboss. Our server uses jboss. I searched the Internet and added the encoding format when connecting to the data source. The code is as follows:
<datasource jta="true" jndi-name="java:jboss/datasources/JcMysqlDS" pool-name="JcMysqlDS" enabled="true" use-java-context="true"><connection-url>jdbc:mysql://46/ITOO_BASIC_BASIC?useUnicode=true&characterEncoding=UTF-8</connection-url><driver>mysql</driver><pool><prefill>false</prefill><use-strict-min>false</use-strict-min><flush-strategy>FailingConnectionOnly</flush-strategy></pool><security><user-name>root</user-name><pass<a href="http://wwwitnet/edu/ebg/" target="_blank">word</a>>123456</pass<a href="http://wwwitnet/edu/ebg/" target="_blank">word</a>></security></datasource>
1. Page garbled
Pages are relatively easy to solve, and they often set up relevant character sets on the corresponding jsp page or html page. like
%@page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%
2. Transfer value garbled code
During the value transmission process, garbled codes also appear frequently. Not to mention what scenario it is, the commonly used solutions include the following configurations specified filters
<!-- Configure the request filter, set the encoding format to UTF-8, avoid Chinese garbled --> <filter> <filter-name>springUtf8Encoding</filter-name> <filter-class>orgspringframeworkwebfilterCharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter>
Set request character set
Often, after passing it from the front desk to the corresponding controller or action, garbled code appears. My general idea is to print the default character set of request itself first.
Systemoutprintln(requestgetCharacterEncoding());
Then, according to the situation, if the required character set is not printed, set the corresponding character set.
requestsetCharacterEncoding("UTF-8");Of course, some situations may not be solved. You can use the following
String str=newString((requestgetParameter("bigQuestionTypeName"))getBytes("iso-8859-1"),"utf-8")3. Save the database garbled code
This is relatively complicated. Here, lz uses MySQL, and mysql is used to introduce how to solve this garbled problem
As we all know, whether it is pure jdbc, hibernate or jpa, it is better to use it at the bottom. In fact, it is essentially jdbc, and the corresponding framework is only encapsulated on a relevant basis. So no matter what kind of technology it is, it will use the URL connected to the database. So the url needs to be checked first
url
The standard situation will add the corresponding character set settings after the corresponding situation, as follows
jdbcUrl=jdbc:mysql:///itcastoa?useUnicode=true&characterEncoding=UTF-8
As mentioned above, useUnicode needs to be connected to the character set in the database, & what is this? This is a problem, in xml & is the escape character of &. If you use XML to configure the corresponding database connection configuration, then that's the problem. But if you use **properties, there will be a problem, and you must remove the amp. This is indeed the experience of lz having a bloody head.
database
The problem here is also relatively difficult to deal with. Log in to the database
View database encoding format
<img src="http://www2ctocom/uploadfile/Collfiles/20150302/png" 7plu ninja htahle ninja: 1opentablaug: Sun. 1 day sql>"="">
You can see whether the character set of server is latin1. Here we need to talk about the commonly used character sets.
For the peace and prosperity of the world, the ISO organization has designated a unicode character set solution. Unicode encoding is a bridge of mutual communication and conversion between different encodings, including 32-bit binary, so it can accommodate characters to the power of 2, which is enough for life. Unicode is divided into three solutions according to different needs.
Utf8: Used to solve the coding that appears in different languages internationally. For English, 8 digits are used, and for Chinese, 3 digits are used. It can be displayed on any browser that supports the utf9 character set without further processing.
The other two are utf16 and 32, which will not be edited here. You can check it yourself, but it is still because of the convenience of storage and use to decide which one to use.
Okay, the one that is relatively familiar is gbk, commonly known as the national standard code, formulated by Chinese national standards, and only contains Chinese characters. So compared with the two, utf8 is better compatibility, but has a larger storage capacity.
Come back soon, roughly, if you charge the character set, or come back and solve the problem first. So it is OK to use gbk or utf8 here. But latin1 is definitely not possible, here it is mainly set by such a command
The server, database and data table parts are set separately, and the connection encoding must be set. The connection encoding settings are as follows:
mysql> SET character_set_client='gbk';mysql> SET character_set_connection='gbk'mysql> SET character_set_results='gbk'
Set up the encoding and you can successfully insert Chinese below. In fact, it can be solved in one sentence.
Commonly used commands
View database encoding format
show variables like 'character_set_%';
View the creation of tables in the database
show create table tablename;
Set database encoding format
setnames 'gbk';
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.