A complete solution to the problem of Spring MVC Chinese garbled codes

  • 2020-05-17 05:29:15
  • OfStack

Messy codes are a headache for people. This paper introduces the solution to the problem of Chinese messy codes of Spring and MVC. The details are as follows:

1: form submission controller to get Chinese parameters after the messy code solution

Note: the jsp page encoding is set to UTF-8

The form form submission method must be post. The spring encoding filter below the get method does not work


<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<form action="${ctx}/user/addUser" name="userForm" method="post">

Modify web.xml to add the encoding filter 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>     
<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: whether the database encoding supports Chinese

Database tables and table fields are correct

Modify the parameter Settings in configuring the connection database:


 <property name="url" value="jdbc:mysql://localhost:3306/dbname?useUnicode=true&characterEncoding=UTF-8"></property>

Case 1:

When you input the jsp page in Chinese, you will find the controller messy code. What you need to set is to add a filter (filter) with 1 code to UTF-8 in the web.xml file. The code is as follows:

Web.xml profile:


<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>

The important thing to note here is that it is best to put this code at the beginning of webxml, because there is a sequence of intercepts, which is easy to miss if you put it later.

Case 2:

Database Chinese data, jsp page shows garbled code (not in the strict sense of garbled code, but in the form of question mark)

Since we used json data for the data interaction in front and background, I am not quite clear about the reason for this situation, and I have never encountered it before. I can only blame myself for the lack of projects I have done, so it is not difficult to solve. I just need to set 1 encoding format when transferring to json, and the code is as follows:


responsesetContentType("application/json;charset=UTF-8");// Prevent data from passing garbled codes 

Write this so that there are no more garbled words.

Case 3:

The page is in Chinese, and it is also correct to pass it to controller, but after saving it to the database, it is garbled (not strictly garbled, but full of question marks like the first one above).

The question has bothered me 1 time, began to feel the database encoding format is not correct, to create a database of coding format for utf - 8 still can't, finally feel is jboss problem, our server is jboss, surf the Internet to check the data when connected to the data source and the coding format is ok, 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" class="keylink">word</a>>123456</pass<a href="http://wwwitnet/edu/ebg/" target="_blank" class="keylink">word</a>>

</security>

</datasource>

1. Scrambled pages

Pages are relatively easy to solve, usually in the corresponding jsp page or html page to set the relevant character set. Such as

%@page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%

2. Sending random Numbers

In the process of value transfer, is also a frequent occurrence of garbled code. Regardless of what the scenario is, there are several commonly used scenarios for filter specified in the following configurations


<!--  Configure the request filter with the encoding format set to UTF-8 , avoid Chinese garbled code -->

  <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>

     <param-name>forceEncoding</param-name>

     <param-value>true</param-value>

    </init-param>

  </filter>

Set the request character set

It is common to get scrambled codes from the foreground to the corresponding controller or after action. The idea of "1" is to print the default character set of request itself


Systemoutprintln(requestgetCharacterEncoding());

Then, as the case may be, if the desired character set is not printed, the appropriate character set can be set


requestsetCharacterEncoding("UTF-8");

Of course, a situation might still not be resolved by using this one


<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>     
<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>
0

3. Stored database garbled code

This is relatively complex. Here, MySQL is used by lz, and mysql is used to introduce how to solve this problem of scrambled codes

As we all know, whether the underlying layer is pure jdbc, hibernate or jpa is fine, but it is essentially jdbc, and the corresponding framework is just a specific encapsulation based on the relevant basis. So whatever the technology is, it's going to be url connected to the database. So url needs to be checked first

url

The standard case will be followed by the corresponding character set, as shown below


<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>     
<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>
1

As mentioned above, useUnicode goes without saying, connect to the character set set in the database, & what is this? This is problematic, in xml & is the escape character for &. This is a problem if you are using xml to configure the corresponding database connection configuration. However, if you use **properties, you will have a problem. Then you must remove amp. This is really lz who had a head-broken experience.

The database

The problem here is also relatively difficult to deal with, login to the database

View the database encoding format

< img width="576" height="378" style="width: 630px; height: 52 px; display: inline;" alt=" the computer generated the optional text: sql > useitcastoaatabasechanged xi sql2status xi sqlUer14. 14 Distribs. 6. 19, forUin64onnectionid: u precision precision entdatabase: LtrrentLtser: SL: singdelimiteP: e protection of ue ue protecting sion: rotocolve precision sion: onnection: e precision uercha precision acterset: bcharacterset: lientcha protection of acte set: onn. cha protection of acte set: CPport: ptine: 1 itcastoarootelocal bligh ostHotinuse; 5. 6. 19H xi SQLCo. unit day 194en1 localhostviaTCP/IPlatinlUtfsgbkgbk33 day 61hour51oin4? 1 Questions sechPeads: S: 6:7 que protection of iesPe second510 beg querie endure: opens: "src =" http: / / www2ctocom uploadfile Collfiles / 20150302 / png "7 plu endure htahle endure: opentablaug: 1 day. Day in, day out > "="" >

You can see that the server character set is still latin1, so we need to talk about the common character set.

For the sake of world peace and prosperity, the ISO organization has designated a set of unicode character set scheme. The Unicode code is a bridge of communication and conversion between different codes, including 32-bit base 2, so it can accommodate 2 to the power of 31 characters, which is enough in one's lifetime. According to different needs, Unicode can be divided into three schemes.

Utf8: code used to deal with different international languages. For English, 8 bits, and for Chinese, 3 bits. Can be displayed on any browser that supports the utf9 character set without additional processing.

The other two are utf16 and 32, which are not listed here. You can consult by yourself, or because the total storage and use of convenience to decide which to use.

Well, the other relatively familiar is gbk, commonly known as national code, China's national standard to formulate, only including Chinese characters. Therefore, utf8 is more compatible than the other two, but it has more storage.

I'll come back in a minute, basically charge the character set or come back and fix the problem first. So gbk or utf8 is fine. However, latin1 is definitely not allowed, which is mainly set by such a command

The encodings for the server, database, and data table sections are set respectively, and the connection encodings must be set. Connection encoding Settings are as follows:


<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>     
<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>
2

After setting the code, you can insert Chinese successfully. In fact, you can use 1 sentence to solve the problem

Common commands

View the database encoding format


show variables like 'character_set_%';

View the creation of tables in the database


<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>     
<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>
4

Set the database encoding format


<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>     
<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>
5

Related articles: